Search code examples
pythonmongodbsqlalchemypymongomongoengine

Classical mapping in MongoEngine


I'm new to MongoEngine and it looks like we need to create sub classes of the class Document from the mongoengine to model our DB. I'm a little concerned here because this violates the Dependency Inversion from the SOLID principles. So if I need to use another database at a later point of time, I will have to change my domain model classes which I shouldn't really be doing.

SQLAlchemy overcomes this by providing a beautiful classical mapping. Using this, the database dependent code is separated from my domain model, so I don't really need to worry about the database provider and I can easily abstract the details away should I have a need to change my database.

Is there a equivalent of this for MongoDB, preferrably in MongoEngine?


Solution

  • Pymongo's official doc provides a list of the existing ORM/ODM and frameworks but to my knowledge they all implement the Active Record Pattern (just like django ORM), which as you said, violates the SOLID principles but is good enough for many simple use cases.

    MongoAlchemy, which was inspired by SQLAlchemy uses a concept of session so it may be closer to what you are looking for but the project is no longer maintained.