1

I know its duplicate question but I am really don't understand why its not working

I need to remove documents from nested array

( example remove item: requirement[0].update[2] )

Schema {
    name: String,
    requirement:[{
        version: Number,
        update:[{
            date: Date,
            number: Number,
            description: String
        }]
    }],
}

code

File.findOneAndUpdate(
    { name: req.params.name, }, 
    //, 'requirement._id': req.body.versionID

    { "$pull":  {'requirement.$.update._id': req.body.versionNumID} },  
    { safe: true, multi:true }, 
    function(err, obj) {}
);

reference

1 Answer 1

2

You can try this:

File.findOneAndUpdate({name:req.params.name},
  {$push: {"requirements.0.update": {_id:req.body.versionNumID}}
});
Sign up to request clarification or add additional context in comments.

3 Comments

not working "requirement."+req.body.versionIndex+".update" adding index
@ShibinRagh Try this: {[‘requirements.${req.body.versionIndex}.update’]: {_id:req.body.versionNumID}}
is there any other solution instead of using index ? , Using "update" array _id ? @dadwic

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.