Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB pullAll objects with multiple arguments

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" }]
}
like image 601
kschaeffler Avatar asked Dec 11 '25 07:12

kschaeffler


1 Answers

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.

like image 84
Asya Kamsky Avatar answered Dec 12 '25 19:12

Asya Kamsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!