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.
You can use findOneAndUpdate with the $pull command.
Example:
User.findOneAndUpdate({/* filter */}, { $pull: {array: {/* condition */}} });
More information:
Wondering if the below would work as well....
Model.findOneAndUpdate({'job.status' : "terminated"}, { $pull: job.$ }, {$multi : true});
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.