I just want to remove several Objects in from my array in mongoDB using pullAll
db.collection.update({'_id': ObjectId(".....")}, { $pullAll : { 'notifications' : [{'type' : type}, {'id': id}]} })
Why is this not working? What is the correct syntax?
Update:
the document is:
{
"_id" : ObjectId("......"),
"notifications" : [ { "type" : "aaa",
"id" : "123" },
{ "type" : "bbb",
"id" : "123" },
{ "type" : "ccc",
"id" : "234" }]
}
Your problem may be one of two places:
First your update has a syntax issue:
db.collection.update({'_id': ObjectId(".....")},
{ $pullAll :
{ 'notifications' : [{'type' : type}, {'id': id}]
}
}
)
should be:
db.collection.update({'_id': ObjectId(".....")},
{ $pullAll :
{ 'notifications' : [{'type' : type, 'id': id}]
}
}
)
Note I removed }, { and joined type and id into a single JSON subdocument.
The other issue is your array elements seem to have id values which are strings of form "123" - are you sure you are passing a string to your update statement? String "123" is not equal to integer 123.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With