PyMongo 与 Django 的 MongoEngine
对于我的一个项目,我更喜欢使用 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 技术交流群。
发布评论
评论(4)
木有鱼丸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 的对象。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这是一个老问题,但偶然发现它,我不认为接受的答案回答了问题。问题不是“什么是 MongoEngine?” - 这是“我为什么应该使用 MongoEngine?”以及这种方法的优点。一般来说,这超出了 Django 到 Python/Mongo 的范围。我的两分钱:
虽然 PyMongo 和 MongoEngine 都返回对象(这并没有错),但 PyMongo 返回需要通过字符串引用其键的字典。 MongoEngine 允许您通过文档数据的类定义模式。然后,它会将文档映射到这些类中,并允许您操作它们。为什么要为无模式数据定义模式?因为在我看来,它清晰、明确,并且更容易编程。您最终不会得到分散在代码中的字典,如果不实际查看数据或运行程序,您就无法分辨其中的内容。对于 MongoEngine 和像 PyCharm 这样的不错的 IDE,输入一个简单的“.”之后对象会通过自动完成告诉您需要知道的所有信息。对于其他开发人员来说,在工作时检查和学习数据模型也变得更加容易,并且会让那些一段时间没有见过代码的人变得更加高效、更快。
此外,对我来说,使用 PyMongo 操作文档的语法(本质上与 mongo 控制台相同)很丑陋、容易出错且难以维护。
这是在 MongoEngine 中更新文档的基本示例,对我来说,非常优雅:
为什么使用 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:
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.