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}})
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,
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