Reputation: 1841
I have a model that looks like this:
contacts: {
phones: [{
phone_id: {
type: ObjectId
},
phone_number: {
type: String
}
}],
shared: {
phones: [{
phone_id: {
type: ObjectId
},
phone_number: {
type: String
},
}]
}
}
I have an endpoint, where I am trying to delete phone numbers matching an id I pass in my query string.
My code looks like this:
Contacts.update({ 'phones._id': req.params.phone_id },
{ "$pull": { "phones": { "_id": req.params.phone_id} }},
{ safe: true, multi:true }, function(err, obj) {
});
Contacts.update({ 'shared.phones._id': req.params.phone_id },
{ "$pull": { "shared.phones": { "_id": req.params.phone_id} }},
{ safe: true, multi:true }, function(err, obj) {
});
The first update that removes the number form the phones array works.
But the second that should remove the number from the 'shared.phones' array does not.
Any idea on what I'm missing?
I tried using the '$' positioning operator like this:
Contacts.update({ 'shared.phones.$._id': req.params.phone_id },
{ "$pull": { "shared.phones.$": { "_id": req.params.phone_id} }},
{ safe: true, multi:true }, function(err, obj) {
});
But that didn't work as well.
Thanks in advance!
Upvotes: 1
Views: 31
Reputation: 23545
As said in a comments
{ $pull: { 'shared.phones._id': req.params.phone_id } }
When it comes to nested array, always use all in one keys, because mongodb do not like to go in more than one level deep.
Upvotes: 1