Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use MongoEngine and PyMongo together

I want to use MongoEngine for my next project. Now I'm wondering whether I could also use PyMongo directly in the same project. Just for the case that I need something very special that is not supported directly via mongoengine.

Are there any doubts that this would work, or that I should not do that!?

like image 878
Thomas Kremmel Avatar asked Aug 22 '12 07:08

Thomas Kremmel


People also ask

Does MongoEngine use PyMongo?

MongoEngine uses PyMongo behind the scenes to manage the connections to the database. It is meant to be an easy entryway for developers used to working with SQLAlchemy or other similar ORMs. This can reduce the learning curve when software engineers are first introduced to MongoDB and its document model.

What is the difference between PyMongo and MongoEngine?

PyMongo is the low-level driver wrapping the MongoDB API into Python and delivering JSON in and out. MongoEngine or other layers like MongoKit map your MongoDB-based data to objects similar to native Python database drivers + SQLAlchemy as ORM.

Why MongoEngine?

MongoEngine is a Document-Object Mapper (think ORM, but for document databases) for working with MongoDB from Python. It uses a simple declarative API, similar to the Django ORM. Documentation available at docs.mongoengine.org - there is currently a tutorial, a user guide and API reference.


1 Answers

Author of MongoEngine here - MongoEngine is built upon pymongo so of course you can drop into pymongo - or use raw pymongo in your code!

There are some document helpers that allow you to access raw pymongo methods in MongoEngine eg:

class Person(Document):
    name = StringField()

# Access the pymongo collection for the Person document
collection = Person._get_collection()
collection.find_one()  # Use raw pymongo to query data
like image 90
Ross Avatar answered Oct 07 '22 07:10

Ross