3

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?

chaudharyp
  • 3,394
  • 3
  • 26
  • 42

4 Answers4

4

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.

A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70
2

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

Marius
  • 2,946
  • 1
  • 18
  • 18
0

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

foxel
  • 165
  • 10
0

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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xlmaster Feb 19 '23 at 22:37
  • looks interesting, why do you recommend to use it? instead to use only pydantic + motor – maudev May 18 '23 at 02:09
  • As mentioned. Its batteries included and its easy to work with. Furthermore, If you want to use the underlying Motor drivers, you can do so by getting an `AsyncIOMotorClient` instance and get a `AsyncIOMotorClientSession` from that and pass the session directly into the ODM models. I especially love this a lot as it allows me to update multiple documents using transactions which currently (at the time of writing) - no ODM supports for python. – Joel Raymann Jul 21 '23 at 16:08