2015-08-15 56 views
0

的問題是:從蒙戈節點本地驅動器陣列拉出多個值

doc1:{ 
    _id:1, 
    array:['a','b','c'] 
}, 
doc2:{ 
    _id:2, 
    array:['a','f','c'] 
} 

我試圖從外地array刪除['a','b'],使用:

.update({_id:1},{$pull:{array:['a','b']}})但它似乎沒有不改變任何東西,我應該使用不同的功能嗎?

+0

可能的重複http://stackoverflow.com/questions/32002691/how-do-i-remove-a-string-from-an-array-in-a-mongodb-document/32002845#32002845 –

回答

1

$pull運營商其實需要的參數作爲「查詢」,所以經常$in適用於這裏:

.update({ "_id": 1 },{ "$pull": { "array": { "$in": ["a","b"] } } },{ "multi": true }) 

這基本上從陣列問到$pull「或者」值「A」或「B」 。

當您需要更新您的語句中的多個文檔時,請不要忘記「多個」選項。

+0

是的,那工作,謝謝! – xShirase

+0

@xhirase當然是的。請記住它是一個「查詢」,並且還記得[「接受」](http://stackoverflow.com/help/accepted-answer)對您的「工作」問題的回答。 –

+0

大聲笑你不能接受10分鐘之前,你已經在2分鐘回答 – xShirase