deleteMany by _id

Hey there community :slight_smile:
I am having trouble with the deleteMany of ids.

Namely, I am passing via api call an array of string [“6507706269daaddf53f7a722”, “6507707069daaddf53f7a723”]

Then, I am handling the api call like so:

else if (request.method === 'DELETE') {
                              const idsToDelete = request.body.selected.map((_id: any) => new ObjectId(_id))
                              console.log(idsToDelete); 
                              //[
                                      //new ObjectId("6507706269daaddf53f7a722"),
                                      //new ObjectId("6507707069daaddf53f7a723")
                               //]
                              await dbProducts.deleteMany({ _id: { in: idsToDelete } }).catch((error) => console.log(error))
                              return response.status(200).json({ message: 'Product successfully deleted!' });
                    }

I get the OK status of the responce, but the ids are not deleted.

I have also tried to create the array for the “in” like so:
const idsToDelete = request.body.selected.map((_id: any) => ObjectId("${_id}"))

but to no avail :frowning:

Can anyone help?

anyone :slight_smile:

Shouldn’t it this be:

await dbProducts.deleteMany({ _id: { $in: idsToDelete } }).catch((error) => console.log(error))

I need to stop coding after midnight, my eye sight gets worse by the minute :slight_smile:

Thank you sir, and sorry for the bother, and if my notification woke you :slight_smile:

Not to worry, I’ve done that many times, the longer you stare at it the less likely you are to see the issue!

2 Likes