How to use MOTOR for MongoDB in big FastAPI applications? #4476
Replies: 2 comments
-
Personally, I would do one of two things here.
If I were you, I would go for the second option. Pymongo is pretty performant, and Motor is based on Pymongo so I would say that it is not "faster" than Pymongo. That would save you the effort from having to make all of your logic and services asynchronous, and just reuse the existing codebase. Fair warning, Pymongo doesn't come with a class-based data representation by default, it's all just dictionaries. But I think you can easily make it a class by deserializing the dictionary using something like Pydantic (which FastAPI has built-in support for). |
Beta Was this translation helpful? Give feedback.
-
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: https://github.com/pprunty/motormongo and documentation: https://motormongo.readthedocs.io/en/latest/. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hello community, thanks for the awesome tool!
We have recently started using FASTAPI in my org. Our DB is MongoDB and currently, we are using MongoEngine ODM to go about our operations. I explored using MOTOR and saw quite better performance because of native FASTAPI's support for async.
However, how do I scale this setup?
Once the API Request reaches API viewfunction in FastAPI app which is naturally defined as ASYNC, there are 3-5 code layers in our Logic Layer which comprises of different classes and functions. And, at the last layer, we are to make queries to our DB. But MOTOR only works inside ASYNC function which is only the view function, how do I make this happen?? How to scale this?
Beta Was this translation helpful? Give feedback.
All reactions