Python 如何使用mongokit在find()之后更新对象?
但是,当我运行它时,我会收到这个对象,它是我的UserDocument的一个实例。但提出了这一例外:Python 如何使用mongokit在find()之后更新对象?,python,mongodb,mongokit,nosql,Python,Mongodb,Mongokit,Nosql,但是,当我运行它时,我会收到这个对象,它是我的UserDocument的一个实例。但提出了这一例外: @connection.register class User(Document): __database__ = 'foo' __collection__ = 'bar' structure = { '_id' : ObjectId, 'Email' : basestring, } col = connection['upla
@connection.register
class User(Document):
__database__ = 'foo'
__collection__ = 'bar'
structure = {
'_id' : ObjectId,
'Email' : basestring,
}
col = connection['uplace']['user']
user = col.find()[2]
user2 = User(user)
user2['Email'] = 'newEmail@rit.edu'
#connection.register([user])
#user['Wall'][1]['Reply'][0]['Message'] = 'test new message'
#print user
user2.save()
正确的方法是:
server_version = tuple(self.connection.server_info()['version'].split("."))
File "/Library/Python/2.7/site-packages/mongokit/document.py", line 628, in __getattribute__
raise ConnectionError('No collection found')
mongokit.mongo_exceptions.ConnectionError: No collection found
user2 = col.User(user)
user2.pop('_id') # you should remove the _id or saving user2 will replace user
user2["Email"] = "newEmail@rit.edu"
user2.save()