3
假設集合是這樣的:如何在mongodb中拉出數組元素(這是文檔)?
db.mytests.find()
{ "_id" : ObjectId("4fb277b89b8295a790efde44"),
"mylist": [
{ "foo1" :"bar1", "foo2" : "bar2" },
{"foo1" : "bar3", "foo2" : "bar4" }
],
"nonlist" : "nonlistVal" }
我想在mylist
其foo1
等於bar1
刪除文檔,閱讀mongodb document about updating後,我用這個:
db.mytests.update({},{$pull:{'mylist':{'mylist.$.foo1':'bar1'}}})
,但失敗了。 爲了弄清楚我插入一個新的數組的問題轉化爲mytests
使用此:
db.mytests.update({},{$set:{'anotherList':[1,2,3,4]}})
,然後使用db.mytests.update({},{$pull:{'anotherList':{$gt:3}}})
拉 4
在陣列anotherList
,它成功的元素。我的問題是mylist.$.foo1
?你能告訴我刪除數組中的文檔元素的正確方法嗎?
你可能要通過多=真,因爲你想更新的所有文件。 –