开发者

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 o开发者_StackOverflow社区bjects, aren't they? So what is the purpose of MongoEngine?


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.


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.


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


mongoengine will use the pymongo driver to connect to mongodb.

If you are familiar with django.. use mongoengine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜