PyMongo 与 Django 的 MongoEngine

发布于 11-01 21:47 字数 212 浏览 6 评论 0原文

对于我的一个项目,我更喜欢使用 Django+Mongo

为什么我应该使用 MongoEngine,而不仅仅是 PyMongo?有什么优点?使用 PyMongo 查询给出的结果是 allready 对象,不是吗?那么 MongoEngine 的目的是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫离伤花2024-11-08 21:47:58

这是一个老问题,但偶然发现它,我不认为接受的答案回答了问题。问题不是“什么是 MongoEngine?” - 这是“我为什么应该使用 MongoEngine?”以及这种方法的优点。一般来说,这超出了 Django 到 Python/Mongo 的范围。我的两分钱:

虽然 PyMongo 和 MongoEngine 都返回对象(这并没有错),但 PyMongo 返回需要通过字符串引用其键的字典。 MongoEngine 允许您通过文档数据的类定义模式。然后,它会将文档映射到这些类中,并允许您操作它们。为什么要为无模式数据定义模式?因为在我看来,它清晰、明确,并且更容易编程。您最终不会得到分散在代码中的字典,如果不实际查看数据或运行程序,您就无法分辨其中的内容。对于 MongoEngine 和像 PyCharm 这样的不错的 IDE,输入一个简单的“.”之后对象会通过自动完成告诉您需要知道的所有信息。对于其他开发人员来说,在工作时检查和学习数据模型也变得更加容易,并且会让那些一段时间没有见过代码的人变得更加高效、更快。

此外,对我来说,使用 PyMongo 操作文档的语法(本质上与 mongo 控制台相同)很丑陋、容易出错且难以维护。

这是在 MongoEngine 中更新文档的基本示例,对我来说,非常优雅:

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

为什么使用 PyMongo? MongoEngine 是您和裸机之间的一层,因此它可能会更慢,尽管我没有任何基准测试。 PyMongo 级别较低,因此您自然拥有更多控制权。对于简单的项目,MongoEngine 可能是不必要的。如果您已经熟悉 Mongo 语法,您可能会发现 PyMongo 比我直观得多,并且编写复杂的查询和更新没有问题。也许您喜欢直接在较低级别上使用字典,并且对额外的抽象层不感兴趣。也许您正在编写一个不属于大系统的脚本,并且您需要它尽可能精简和快速。

争论还有更多内容,但我认为这对于基础知识来说非常好。

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.

木有鱼丸2024-11-08 21:47:58

我假设您还没有阅读 MongoEngine 声明。

MongoEngine 是一个文档对象
Mapper(想想 ORM,但对于文档
数据库)用于使用 MongoDB
来自Python。

这基本上说明了一切。

另外:你声称 Pymongo 将传递对象是错误的......在 Python 中一切都是对象 - 甚至字典也是一个对象......所以你是对的,但不是在定义自定义类的意义上应用级别。

PyMongo 是将 MongoDB API 包装到 Python 中并传入和传出 JSON 的低级驱动程序。

MongoEngine 或 MongoKit 等其他层将基于 MongoDB 的数据映射到类似于本机 Python 数据库驱动程序 + SQLAlchemy 作为 ORM 的对象。

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.

逆流2024-11-08 21:47:58

可能为时已晚,但对于其他尝试 Django+Mongo 的人来说, Django-nonrel 值得考虑。

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

小兔几2024-11-08 21:47:58

mongoengine 将使用 pymongo 驱动程序连接到 mongodb。

如果你熟悉 django.. 使用 mongoengine

mongoengine will use the pymongo driver to connect to mongodb.

If you are familiar with django.. use mongoengine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文