问题描述
在与Mongo进行交互时,我一直在使用Python API来使用。我做了一个关于景观的快速调查,并确定了三位主要候选人。
I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.
- PyMongo
- MongoEngine
- Ming
如果您正在使用框架,你会选择什么API,为什么?
If you were designing a new content-heavy website using the django framework, what API would you choose and why?
MongoEngine看起来像是专门用Django构建的。 PyMongo似乎是Mongo周围的一个薄薄的包装。它有很多的权力,尽管通过使用django作为一个框架失去了大量的抽象。明代表是PyMongo和MongoEngine之间的一个有趣的中间地带,尽管我没有机会参加考试。
MongoEngine looks like it was built specifically with Django in mind. PyMongo appears to be a thin wrapper around Mongo. It has a lot of power, though loses a lot of the abstractions gained through using django as a framework. Ming represents an interesting middle ground between PyMongo and MongoEngine, though I haven't had the opportunity to take it for a test drive.
推荐答案
p>正如Mike所说,你不能避免PyMongo - 所有其他接口都建立在它之上。这些其他接口可以说是不必要的。在处理SQL时,ORMs在处理SQL时很有用,因为它们可以减轻创建SQL查询和模式的复杂性,并将结果集解析成对象。
As Mike says, you can't avoid PyMongo - all the other interfaces build on top of it. These other interfaces are arguably unnecessary. ORMs such as that used in Django are useful when dealing with SQL because they mitigate the complexity of creating SQL queries and schemas, and parsing result sets into objects.
PyMongo已经已经覆盖 - 查询通过一个方便和简单的API,并且来自MongoDB的结果已经是对象(嗯,Python中的dict - 同样的区别)。如果您觉得您真的需要使用Python对象来装饰您的Mongo文档,则可以轻松地向PyMongo添加SON操纵器。这个方法的好处在于,您可以直接在PyMongo上编写代码,稍后再插入其他功能,而无需在代码和PyMongo之间插入新的API。
PyMongo however already has that covered - queries go through a convenient and simple API and results coming from MongoDB already are objects (well, dicts in Python - same difference) by definition. If you feel that you really need to decorate your Mongo documents with Python objects, it's easy to add a SON manipulator to PyMongo. The nice thing about this approach is that you can write code directly on PyMongo, and slide in additional functionality later on without having to insert a new API between your code and PyMongo.
剩下了什么?模式创建和迁移有些有用,但是几乎完全是特别的 - 如果您正在考虑使用MongoDB,您将要破坏传统的SQL风格模型。另外,如果有一个完全Django兼容的MongoDB ORM,你可能会得到一些里程。任何不到这一点,你可能会为自己创造工作。
What's left? Schema creation and migration are somewhat useful, but are almost as simply done ad-hoc - chances are if you're considering using MongoDB you want to break out of the traditional SQL-style model anyway. Also, if there were a fully Django-compatible MongoDB ORM you might get some mileage out of it. Anything less than that and you will probably be creating work for yourself.
你不会后悔直接使用PyMongo。
You won't regret using PyMongo directly.
如果您对最高效率感兴趣,最后一个值得注意的是PyMongo的异步版本,其中:
One last option worth watching if you are interested in top efficiency is the asynchronous version of PyMongo, here: http://github.com/fiorix/mongo-async-python-driver
这篇关于Mongo DB和Django应该使用哪个Python API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!