5

I want to pull a specific item from an embedded array...assume the following mongo document....

db.test.find()

{
  id:1,
  comments : 
   [
     { cid: 1 },
     { cid: 2 },
     { cid: 3 },
     { cid: 4 },
     { cid: 5 }
   ]
}

I want to remove an item from the comments array by cid, not by position. I've tried all of these but none of them appear to work. I've tried using the dot notation, but that does not seem to have any effect. I tried the last post suggestion from How to Delete-nth element from array, but no luck...

db.test.update({ 'comments.cid' : 5}, {"$pull" :{"comments":{"cid":"3"}}}    )
db.test.update(  {id: 1}, {"$pull" : {"comments" : { "cid" : "3"}}},false,false)
db.test.update(  {id: 1}, {"$pull" :{"comments.cid" :3}})

3 Answers 3

12

this should work:

db.test.update(  {id: 1}, {$pull :{comments: {cid :3}}})

also, in your document, you have: id: 1 without comma at the end, it shold be:

id:1, 
Sign up to request clarification or add additional context in comments.

5 Comments

This worked great, thank you...is it possible to take this one step further using findAndModify where I want to remove the array item only if the document exists, and, the array item exists? In other words, findAndModify( query:{id:1, $where "comments.cid = 3"}, ...
I think that with the query you already have it will remove it only if document with id 1 exists and comment with cid exists...how it would remove it anyway? :) or did I get you wrong?
yes, you're right, but in my document, I also have a commentsCounter, I did not mention this at first, it keeps track of the number of items in the comments array, so when I use findAndModify, I want to remove the item from the array and decrement commentsCounter only if the item exists, but if it does not, make no changes to the document at all, not the array or the counter
got it..not sure how that can be done though...perhaps you will have to use mongodb.org/display/DOCS/Server-side+Code+Execution for that purpose...not sure if findAndModify will help.
@stackoverflow.com/users/45725/vucetica I'll use you original suggestion and answer as a starting point, thanks for the help!
2

These worked too...

db.test.update({comments:{cid:4} }, 
                    {$pull:{comments:{cid:4}},  
                      $inc:{commentCount: -1}})

db.test.update({"comments.cid" : 17}, 
                     {$pull:{ comments:{cid: 17}}, 
                      $inc:{commentCount:-1}})

1 Comment

How to pass Array of condition like db.test.update({"comments.cid" : 17}, {$pull:{ comments: {$in: [{cid: 17}, {cid: 18}]}, $inc:{commentCount:-1}}) isn't working.
-2

Just wanted to modify the answer so that it can delete multiple objects from an array.

db.test.update(  {id: 1}, {"$pullAll" : {"comments" : [{ "cid" : "3"},{ "cid" : "2"}]}})

This answer has been updated and it works with mongoose too

Comments

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.