Home > Mobile >  Mongoose deleteMany using _id on nodejs
Mongoose deleteMany using _id on nodejs

Time:12-21

i have a ids of array ['123','456', '789']. i want delete all this array in mongodb

How i user it:

ScheduleModel.deleteOne({ _id: ['123','456', '789'] });

this is not working because this is not object Id

what i Need :

ScheduleModel.deleteOne({ _id: [ObjectId('123'), ObjectId('456'), ObjectId('789')] });

How to add object Id in array data. any how to resolve this issues. i need a solution on this.

CodePudding user response:

It's not about ObjectId. you are using wrong syntax. You must use $in statement

ScheduleModel.deleteMany({ id: { $in: ['123','456','789'] } });

CodePudding user response:

Use deleteMany with proper syntax.When there are many objectId at time Use $in.

Try this :

var deleteCondition = {
            _id : {
                   //In Array you can pass your objectId
                    $in : ['123','456','789']
                 }
                //You can pass other conditions
        }
    
        //deleteMany
        ScheduleModel.deleteMany(deleteCondition, function (err, res) {
                            if (res) console.log(res)
                        })
  • Related