Interested in using MongoDB for my DB - Looking for Async ODM Solution for Python

Hi there! As the title suggests, I’m looking for an Asynchronous ODM abstraction for use specifically with FastAPI. My language of choice is Python.

So far I’ve seen Beanie and ODMantic but wanted to go straight to the source and get advice from experienced MongoDB devs. What do you all think? Anything else to know before diving in?

Welcome to the MongoDB Community Forums @Anthony_Polyak !

Beanie and ODMantic both build on similar dependencies:

  • Motor - officially supported asynchronous Python driver for MongoDB
  • Pydantic - data validation and settings management using Python type annotations

I’m also aware of μMongo: sync/async ODM, which predates Beanie and ODMantic by 4 or 5 years and uses Marshmallow for serialisation instead of Pymantic. Beanie is the newest of the ODMs mentioned here – the initial release was just under a year ago, with 40+ releases since then.

There may be other Python async ODMs, but these are the ones I see most commonly mentioned in community discussion (which is far less frequently than folks using the Motor async driver directly in my experience).

If you are just getting started with MongoDB I would consider using Motor directly. ODMs try to improve developer experience/convenience but there is a performance and troubleshooting tradeoff given extra layers of abstraction and potential overhead.

Any of the mentioned libraries will work with FastAPI, including using Motor directly: Getting Started with MongoDB and FastAPI.

Regards,
Stennie

2 Likes

I have built an asynchronous ODM for MongoDB on-top of Motor, called “motormongo”. It has a similar API to mongoengine. Making it easy for user’s of mongoengine users to transition to use motormongo asynchronous capabilities. It is designed specifically for use in FastAPI projects. You can find the source code here: GitHub - pprunty/motormongo: 🤖 An Object Document Mapper (O.D.M) for MongoDB built on-top of Motor's asynchronous library for asynchronous CRUD operations. Built for use in asynchronous backend applications using Python. and documentation: motormongo — motormongo 0.1.13 documentation. Hope this helps!