2014-08-30 104 views
1

這是可能做到這一點的短片在Python,我得到的錯誤,Pymongo子文檔更新

>> collection.insert({u'name.first': u'John'}) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/someone/env/local/lib/python2.7/site-packages/pymongo/collection.py", line 415, in insert 
    self.uuid_subtype, client) 
bson.errors.InvalidDocument: key 'name.first' must not contain '.' 

編輯:感謝shx2指着我出來,那插入應該更新。而在網絡上的一些衝浪後,我找到了答案是,

>> collection.update({_id: <some_id_or_index>}, {$set: {<python_dict_object_to_be_updated>}} 

回答

2

什麼你想要做的是更新一個(的子文檔)現有的文檔,併爲此你應該使用update而不是insert

insert用於向集合添加新的文檔。