3

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" }]
}
2
  • What's the structure of your document ? Commented Nov 19, 2012 at 17:25
  • Have a look at this link stackoverflow.com/questions/10310837/… Commented Nov 19, 2012 at 17:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

To use $pullAll you need to match the whole object exactly. Why not just use $pull, I'm sure it would suit your needs

2 Comments

I just tried with db.collection.update({'_id': ObjectId(".....")}, { $pull : { 'notifications' : [{'type' : type}, {'id': id}]} },False,True) and db.collection.update({'_id': ObjectId(".....")}, { $pull : { 'notifications' : {'type' : type , 'id': id}} },False,True) and I have the same result
Try and $pull using only "type" or only "id", just to see if it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.