0

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.

Aravinth E
  • 449
  • 2
  • 6
  • 29
  • 1
    Does this answer your question? [Mongoose - remove multiple documents in one function call](https://stackoverflow.com/questions/44467318/mongoose-remove-multiple-documents-in-one-function-call) – eol Dec 17 '21 at 20:55

2 Answers2

2

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

ScheduleModel.deleteMany({ id: { $in: ['123','456','789'] } });
Soroush Bgm
  • 482
  • 2
  • 15
0

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)
                        })
Prit Hirpara
  • 168
  • 10