How to update a an object after find() with mongokit?

67 views
Skip to first unread message

Kevin Carbone

unread,
Mar 18, 2013, 3:23:04 PM3/18/13
to mong...@googlegroups.com
@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'] = 'newE...@rit.edu'
#connection.register([user])
#user['Wall'][1]['Reply'][0]['Message'] = 'test new message'
#print user
user2.save()

However when I run this, I receive the object and it is a instance of my UserDocument. But this exception is raised:

    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

Nicolas Clairon

unread,
Mar 19, 2013, 4:56:35 AM3/19/13
to mong...@googlegroups.com
The right way is :

    user2 = col.User(user)
    user2.pop('_id')  # you should remove the _id or saving user2 will replace user
    user2["Email"] = "newE...@rit.edu"
    user2.save()



--
 
---
You received this message because you are subscribed to the Google Groups "MongoKit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongokit+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages