A MongoDB ODM based on Motor and Pydantic.
The project code is hosted on GitHub, documentation on ReadTheDocs.
pip install Motor-ODM
from motor.motor_asyncio import AsyncIOMotorClient
from motor_odm import Document
# Create a custom model by subclassing Document
class User(Document):
class Mongo:
# Set the collection name
collection = "users"
# Add attributes to your model
username: str
age: int
# Connect your model to a database
client = AsyncIOMotorClient(...)
Document.use(client.get_default_database())
# Create documents and save them to the database
u = User(username="John", age=20)
await u.insert()
# Query the database
async for user in User.all():
print(user.username)
For a more complete overview have a look at the docs.