Is there a way to define a MongoDB schema using Motor?

Question:

There is a way to define MongoDB collection schema using mongoose in NodeJS. Mongoose verifies the schema at the time of running the queries.

I have been unable to find a similar thing for Motor in Python/Tornado. Is there a way to achieve a similar effect in Motor, or is there a package which can do that for me?

Asked By: chaudharyp

||

Answers:

No there isn’t. Motor is a MongoDB driver, it does basic operations but doesn’t provide many conveniences. An Object Document Mapper (ODM) library like MongoTor, built on Motor, provides higher-level features like schema validation.

I don’t vouch for MongoTor. Proceed with caution. Consider whether you really need an ODM: mongodb’s raw data format is close enough to Python types that most applications don’t need a layer between their code and the driver.

Currently (2019) this project Umongo https://github.com/Scille/umongo seems the more active and usefull if you need a sync/async Python MongoDB ODM. It work with multiple drivers like PyMongo or Motor for async.
Doc is here: http://umongo.readthedocs.io

Answered By: Marius

Also you can use ODMantic as it has the best documentation and the engine supports motor client.

Answered By: foxel

Currently (2023), Beanie [Github Link] is the best ODM I have ever used. It works beautifully with FASTAPI and is deeply integrated with Pydantic making it very easy to model data models.

They have a very nice documentation given in here

Answered By: Joel Raymann