2

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.

1
  • None of the following work. I am using Mongoose. Commented Jul 17, 2020 at 15:20

3 Answers 3

2

You can use findOneAndUpdate with the $pull command.

Example:

User.findOneAndUpdate({/* filter */}, { $pull: {array: {/* condition */}} });

More information:

Sign up to request clarification or add additional context in comments.

Comments

0

Wondering if the below would work as well....

Model.findOneAndUpdate({'job.status' : "terminated"}, { $pull: job.$ }, {$multi : true});

1 Comment

you will have to give people the exact error that you are getting, so somebody can help.
0

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);

3 Comments

@j15 What is the exact code you tried, and what error are you getting in the console, please update the question
I figured it out, jsut had to make a simple change.,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.