Reputation: 23
I have tried a lot of things but can't seem to get any of them to work as intended. Let's say I had something like the following.
{
_id: '12345',
name: 'John Smith',
job: [
{
title: 'Web Developer',
years: '12',
status: 'not active',
},
{
title: 'supervisor',
years: '15',
status: 'terminated',
},
{
title: 'lead developer',
years: '3',
status: 'not active',
},
{
title: 'Software Engineer',
years: '9',
status: 'active',
},
]
}
How can I remove the object with the status of 'terminated'? Also, would it be the same to remove all objects with that status of 'not active'?
Thanks.
Upvotes: 2
Views: 58
Reputation: 5853
Remove nested document using the $pull
operator
var query = {
_id: "12345"
};
var update = {
$pull: {
job: {
status: {
$in: ['terminated', 'not active']
}
}
}
};
db.collection.update(query, update);
Upvotes: 0
Reputation: 55
Wondering if the below would work as well....
Model.findOneAndUpdate({'job.status' : "terminated"}, { $pull: job.$ }, {$multi : true});
Upvotes: 0
Reputation: 980
You can use findOneAndUpdate with the $pull command.
Example:
User.findOneAndUpdate({/* filter */}, { $pull: {array: {/* condition */}} });
More information:
Upvotes: 2