2015-10-17 40 views
8

我使用MongoDB作爲Python Web應用程序(PyMongo + Bottle)的後端數據庫。用戶可以上傳文件並選擇「標記」這些文件。這些標籤按照以下方式存儲在文檔中:將項目添加到PyMongo中的MongoDB文檔數組而不重新插入

{ 
    "_id" : ObjectId("561c199e038e42b10956e3fc"), 
    "tags" : [ "tag1", "tag2", "tag3" ], 
    "ref" : "4780" 
} 

我試圖讓用戶在任何文檔中追加新標籤。我想出了這樣的事情:(。FYI; ref關鍵始終是唯一的,這可能很容易被_id以及)

def update_tags(ref, new_tag) 
    # fetch desired document by ref key as dict 
    document = dict(coll.find_one({'ref': ref})) 
    # append new tag 
    document['tags'].append(new_tag) 
    # re-insert the document back into mongo 
    coll.update(document) 

好像應該有一個辦法只有更新「標籤」值直接不用拉回整個文件並重新插入。我在這裏錯過了什麼嗎?

任何想法是極大的讚賞:)

回答

12

你並不需要使用首先檢索該文件只是用.update方法與$push運營商。

def update_tags(ref, new_tag): 
    coll.update({'ref': ref}, {'$push': {'tags': new_tag}}) 

由於更新已經過時,你應該,如果你正在使用pymongo 2.9或更新版本

+1

兩者有什麼區別?只是返回值(文檔vs UpdateResult對象)?你什麼時候使用其中一種? – stackoverflowwww

+4

如果'標籤'字段不存在會發生什麼? –

+0

如果'tags'字段不存在,則會創建它。 @GauravOjha – styvane

0

只需添加到@ssytvane答案使用find_one_and_updateupdate_one方法,並回答@Guarav:您可以添加「 UPSERT = TRUE」,如果它不存在:

def update_tags(ref, new_tag): 
    coll.update({'ref': ref}, {'$push': {'tags': new_tag}}, upsert = True) 

def update_tags(ref, new_tag): 
    coll.update_one({'ref': ref}, {'$push': {'tags': new_tag}}, upsert = True) 
+0

感謝您對Stack Overflow的貢獻。請注意,僅限代碼答案是因爲他們沒有解釋他們如何解決問題而不鼓勵。請不要參考其他答案/評論,而應考慮更新答案,以解釋其做法和解決方法,以便它是一個獨立答案,並且在其他答案或評論被移除的情況下仍然有意義。 – FluffyKitten

1

你可以簡單地做

1)如果你想添加一個條目

def update_tags(ref, new_tag): 
    coll.update({'ref': ref}, {'$push': {'tags': new_tag}}) 

如:

{ 
    "_id" : ObjectId("561c199e038e42b10956e3fc"), 
    "tags" : [ "tag1", "tag2", "tag3" ], 
    "ref" : "4780" 
} 
>> update_tags("4780", "tag4") 
{'updatedExisting': True, u'nModified': 1, u'ok': 1, u'n': 1} 
>> coll.find_one({"ref":"4780"}) 
{ 
    "_id" : ObjectId("561c199e038e42b10956e3fc"), 
    "tags" : [ "tag1", "tag2", "tag3" , "tag4" ], 
    "ref" : "4780" 
} 

2)如果要追加多個條目

def update_tags(ref, new_tag): 
    coll.update({'ref': ref}, {'$pushAll': {'tags': new_tag}}) #type of new_tag is list 

例如:

{ 
    "_id" : ObjectId("561c199e038e42b10956e3fc"), 
    "tags" : [ "tag1", "tag2", "tag3" ], 
    "ref" : "4780" 
} 
>> update_tags("4780", ["tag5", "tag6", "tag7"]) 
{'updatedExisting': True, u'nModified': 1, u'ok': 1, u'n': 1} 
>> coll.find_one({"ref":"4780"}) 
{ 
    "_id" : ObjectId("561c199e038e42b10956e3fc"), 
    "tags" : [ "tag1", "tag2", "tag3" , "tag4" , "tag5", "tag6", "tag7" ], 
    "ref" : "4780" 
} 

注意:如果密鑰不存在,mongo會創建新的密鑰。

相關問題