问题描述
我想使用 pymongo 连接和方法来处理 mongodb,但同时我想使用 mongoengine ORM.
I want to use pymongo connection and methods to work with mongodb, but at the same time i want using mongoengine ORM.
示例:
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
john = User(email='[email protected]')
john.first_name = 'Jonh'
john.last_name = 'Hope'
现在我想将新形成的文档用户插入到我的test_collection"中.如果只使用 mongoengine 我可以这样做:
And now i want to insert new formed document User into my 'test_collection'.In case using only mongoengine i can do this:
connect('test_database')
john.save()
然后我可以轻松访问我的数据:
And then i can make easy accessing my data:
for user in User.objects:
print user.first_name
但是当我尝试使用 pymongo 做同样的事情时,我遇到了一个错误:
But when i'm trying to do same using pymongo, i've got an error:
connection = MongoClient('localhost', 27017)
db = connection.test_database
collection = db.test_collection
collection.insert(john)
追溯:
Traceback (most recent call last):
File "C:/Users/haribo/PycharmProjects/test/mongoeng.py", line 18, in <module>
collection.insert(john)
File "C:\Python27\lib\site-packages\pymongo\collection.py", line 353, in insert
docs = [self.__database._fix_incoming(doc, self) for doc in docs]
File "C:\Python27\lib\site-packages\pymongo\database.py", line 258, in _fix_incoming
son = manipulator.transform_incoming(son, collection)
File "C:\Python27\lib\site-packages\pymongo\son_manipulator.py", line 73, in transform_incoming
son["_id"] = ObjectId()
TypeError: 'str' object does not support item assignment
但这有效:
connection = MongoClient('localhost', 27017)
db = connection.test_database
collection = db.test_collection
john = { 'email': '[email protected]', 'first_name': 'Jonh', 'last_name': 'Hope' }
collections.insert(john)
并使用 pymongo 访问数据:
And accessing data using pymongo:
print collection.find_one()
{u'last_name': u'Hope', u'first_name': u'Jonh', u'_id': ObjectId('513d93a3ee1dc61390373640'), u'email': u'[email protected]'}
所以,我的主要想法和问题是:
So, my main idea and question is:
如何将 mongoengine 用于 ORM 和 pymongo 连接以及使用 mongodb 的方法?
How i can using mongoengine for ORM and pymongo connection and methods to working with mongodb?
附言我提到我想在某些情况下在金字塔中使用它.
P.S. I mentioned that i want use it in pyramid for some context.
推荐答案
Pymongo 使用 Python 字典(或类似字典的对象)并将它们持久化到 mongoDB.Pymongo 不能接受 MongoEngine 类实例并将它们持久化.
Pymongo takes python dictionaries (or dictionary like objects) and persists them to mongoDB. Pymongo cannot take MongoEngine class instances and persist them.
MongoEngine 是一个 orm 包装器,它在下面使用 pymongo 并在 Class 实例上有一个方法来保存,例如:john.save()
在 MongoEngine 代码中发生的事情是它从 john.save()
code>john 实例转换成字典,可以通过 pymongo 存储在 MongoDB 中.
MongoEngine is an orm wrapper that uses pymongo underneath and has a method on the Class instance to save eg: john.save()
What happens inside the MongoEngine code is it converts the data from the john
instance into a dictionary that can be stored in MongoDB via pymongo.
这篇关于pymongo 使用 mongoengine ORM 在金字塔中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!