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}})
Community
  • 1
  • 1
raffian
  • 31,267
  • 26
  • 103
  • 174

3 Answers3

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, 
Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
  • 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"}, ...` – raffian Feb 05 '12 at 04:47
  • 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? – Aleksandar Vucetic Feb 05 '12 at 04:50
  • 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 – raffian Feb 05 '12 at 04:58
  • got it..not sure how that can be done though...perhaps you will have to use http://www.mongodb.org/display/DOCS/Server-side+Code+Execution for that purpose...not sure if findAndModify will help. – Aleksandar Vucetic Feb 05 '12 at 05:04
  • @http://stackoverflow.com/users/45725/vucetica I'll use you original suggestion and answer as a starting point, thanks for the help! – raffian Feb 05 '12 at 05:06
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}})
raffian
  • 31,267
  • 26
  • 103
  • 174
  • 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. –  May 10 '17 at 11:51
-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

Amit Kumar
  • 407
  • 5
  • 7