Error when using mongo deletemany function in nodejs

So this one is likely to be a simple solution but has me tearing my hair out.

I am using Cypress to drive my automation and removing documents from collections using delete many.

I had been using the delete all documents option {} however I now need some data to persist so only want to delete a subset of ids:

    for (var x in $data) {
        var id = $data[x]["_id"]["$oid"];
        arr.push(new ObjectId(id));
    }
    _collectionQuery = { _id: { $in: arr } };

the data is structured as follows:

[
  {
    "_id": {
      "$oid": "56f3ff91b760388cb9e70cbd"
},
  ...
}

]

When I run the script I get an error:

" object [{"_bsontype":"ObjectID","id":{"type":"Buffer","data":[86,243,255,145,183,96,56,140,185,231,12,189]}}] is not a valid ObjectId"

Fairly obvious what the error means but I am unsure what I am doing wrong?



Read more here: https://stackoverflow.com/questions/66332516/error-when-using-mongo-deletemany-function-in-nodejs

Content Attribution

This content was originally published by petermartin2002 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: