Reputation: 4331
I have the following object in my database:
{
"_id": "fTgR2YtHiZBzzqF6J",
"following": [
{
"user": {
"_id": "S4dLHRJiuHoyAp26q",
"fb": {
"id": "100006681067911",
"name": "Helen Amffhajfgiaa Laubergskymanwitzescusonsteinsen"
}
},
"date": "2013-10-01T17:25:50.305Z"
},
{
"user": {
"_id": "MAyxz4Yk5F9vh9RRy",
"fb": {
"id": "100006719587007",
"name": "Mary Amfgaiehgkg Smithman"
}
},
"date": "2013-10-11T10:47:58.898Z"
}
]
}
Now I want to remove the
{
"user": {
"_id": "MAyxz4Yk5F9vh9RRy",
"fb": {
"id": "100006719587007",
"name": "Mary Amfgaiehgkg Smithman"
}
},
"date": "2013-10-11T10:47:58.898Z"
}
subdocument from the array called "following".
My query to do this looks like this:
Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
{
"$pull": {
"following": {
"user": {
"_id": "MAyxz4Yk5F9vh9RRy"
}
}
}
});
But nothing happens!
Can someone point me towards my error?
Upvotes: 1
Views: 4579
Reputation: 85
You can also try this answer (Remove Item from Array in Meteor.js), I think it's a better fit, with less code.
Upvotes: 0
Reputation: 4331
My problem was: I had to $pull the whole object:
Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
{
"$pull": {
"following": {
"user": {
"_id": "MAyxz4Yk5F9vh9RRy",
"fb": {
"id": "100006719587007",
"name": "Mary Amfgaiehgkg Smithman"
}
},
"date": "2013-10-11T10:47:58.898Z"
}
}
}
});
and can NOT just call this:
Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
{
"$pull": {
"following": {
"user": {
"_id": "MAyxz4Yk5F9vh9RRy"
}
}
}
});
Upvotes: 4
Reputation: 23644
The problem is exactly the same as https://stackoverflow.com/a/17779687/149818. You query in general right and would be work just for case if you have exactly 1 "user"
, but for multiple users you have to redesign your collection
Upvotes: 0