kirpt
kirpt

Reputation: 845

Mongoose pull nested array

My user schema is

{
  widgets: [
    {
      commands: [
        {
          name: 'delete'
        }
      ]
    }
  ],
  name: 'John',
}

and I want to delete widgets.commands by id. I use mongoose. I know the id but when I make the pull request it doesn't delete it. $pull: {widgets.$.commands: {_id: req.params.id}} Any suggestions?

Upvotes: 3

Views: 2898

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23515

Here you go

 update({}, {
     $pull: {
        'widgets.commands._id': req.params.id,
     },
 });

$pull mongoDB documentation

@example from doc

db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

Upvotes: 1

Related Questions