AIOMotorEngine MongoDB Asyncio ORM

AIOMotorEngine is a port (for asyncio’s event loop) of the MotorEngine which is port of the MongoEngine. The most part of the code is based on MotorEngine. A few classes and functions were rewritten using async and await for asynchronous code (this syntax was introduced in python 3.5). All tests are rewritten too and seem to pass. AIOMotorEngine does not require Tornado but Motor still does.

Supported Versions

AIOMotorEngine is compatible and tested against python 3.5.

AIOMotorEngine requires MongoDB 2.2+ due to usage of the Aggregation Pipeline.

The tests of compatibility are always run against the current stable version of MongoEngine.

Defining Documents

Defining a new document is as easy as:

from aiomotorengine import Document, StringField

class User(Document):
    __collection__ = "users"  # optional. if no collection is specified, class name is used.

    first_name = StringField(required=True)
    last_name = StringField(required=True)

    def full_name(self):
        return "%s, %s" % (self.last_name, self.first_name)

AIOMotorEngine comes baked in with the same fields as MotorEngine.


Let’s see how to use AIOMotorEngine:

import asyncio
import pymongo
from aiomotorengine import connect, Document, StringField, IntField

class User(Document):
    __collection__ = 'users'

    name = StringField(required=True, unique=True)
    age = IntField()

    def __repr__(self):
        return '<User: {0}({1})>'.format(, self.age)

async def create():
    user = await User.objects.create(name='Python', age=13)
    user2 = User(name='Linux', age=20)
    print("_id of user2 is {0}".format(user2._id))

async def query():
    cursor = User.objects.filter(age__gt=10)
    cursor.order_by(, pymongo.DESCENDING)
    users = await cursor.find_all()

loop = asyncio.get_event_loop()
connect('xxx', io_loop=loop)