Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo vs MongoEngine for Django

For one of my projects I prefered using Django+Mongo.

Why should I use MongoEngine, but not just PyMongo? What are advantages? Querying with PyMongo gives results that are allready objects, aren't they? So what is the purpose of MongoEngine?

like image 737
megido Avatar asked Apr 19 '11 07:04

megido


People also ask

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

Can I use PyMongo with Django?

PyMongo is the official distribution recommended by Python to connect your Django application to a MongoDB database. PyMongo is an open-source Python distribution containing tools for interacting with MongoDB. It makes it easier to perform CRUD operations (Create, Read, Update, and Delete) with a MongoDB database.

Does MongoDB work well with Django?

Django, the most popular Python web framework, is an ideal tool to build secure and easy-to-maintain applications using MongoDB. Using MongoDB with Django is advantageous because: Every second, more and more unstructured data is generated from various sources like chats, real-time streams, feeds, and surveys.


3 Answers

This is an old question but stumbling across it, I don't think the accepted answer answers the question. The question wasn't "What is MongoEngine?" - it was "Why should I use MongoEngine?" And the advantages of such an approach. This goes beyond Django to Python/Mongo in general. My two cents:

While both PyMongo and MongoEngine do both return objects (which is not wrong), PyMongo returns dictionaries that need to have their keys referenced by string. MongoEngine allows you to define a schema via classes for your document data. It will then map the documents into those classes for you and allow you to manipulate them. Why define a schema for schema-less data? Because in my opinion, its clear, explicit, and much easier to program against. You don't end up with dictionaries scattered about your code where you can't tell what's in them without actually looking at the data or running the program. In the case of MongoEngine and a decent IDE like PyCharm, typing a simple "." after the object will tell you all you need to know via auto-complete. It's also much easier for other developers coming in to examine and learn the data model as they work and will make anybody who hasn't seen the code in a while more productive, quicker.

Additionally, to me, the syntax used to manipulate documents with PyMongo (which is essentially the same as the mongo console) is ugly, error prone, and difficult to maintain.

Here is a basic example of updating a document in MongoEngine, which to me, is very elegant:

BlogPost.objects(id=post.id).update(title='Example Post') 

Why use PyMongo? MongoEngine is a layer between you and the bare metal, so it's probably slower, although I don't have any benchmarks. PyMongo is lower level, so naturally you have more control. For simple projects, MongoEngine might be unnecessary. If you're already fluent in Mongo syntax, you may find PyMongo much more intuitive than I do and have no problem writing complex queries and updates. Perhaps you enjoy working directly with dictionaries on that lower level and aren't interested in an additional layer of abstraction. Maybe you're writing a script that isn't part of a big system, and you need it to be as lean and as fast as possible.

There's more to the argument, but I think that's pretty good for the basics.

like image 184
Londo Avatar answered Sep 17 '22 23:09

Londo


I assume you have not read the MongoEngine claim.

MongoEngine is a Document-Object Mapper (think ORM, but for document databases) for working with MongoDB from Python.

This basically say it all.

In addition: your claim that Pymongo would deliver objects is wrong....well in Python everything is an object - even a dict is an object...so you are true but not in the sense of having a custom class defined on the application level.

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.

like image 26
Andreas Jung Avatar answered Sep 17 '22 23:09

Andreas Jung


Probably way too late, but for anyone else attempting Django+Mongo, Django-nonrel is worth considering.

like image 42
dragonx Avatar answered Sep 18 '22 23:09

dragonx