Assume the assembly is as follows:
db.mytests.find()
{ "_id" : ObjectId("4fb277b89b8295a790efde44"),
"mylist": [
{ "foo1" :"bar1", "foo2" : "bar2" },
{"foo1" : "bar3", "foo2" : "bar4" }
],
"nonlist" : "nonlistVal" }
I want to delete the document in mylistwhose foo1equal bar1, after reading the mongodb update file . this:
db.mytests.update({},{$pull:{'mylist':{'mylist.$.foo1':'bar1'}}})
but it failed. To find out the problem, I insert a new array into mytestsusing this:
db.mytests.update({},{$set:{'anotherList':[1,2,3,4]}})
and then using db.mytests.update({},{$pull:{'anotherList':{$gt:3}}})to pull an element
4in an array anotherList, it succeeds.
I assumed that the problem is with mylist.$.foo1? Can you tell me the correct way to delete a document element in an array?
source
share