MongoDb not deleting a record by _id, deletedCount: 0

  Kiến thức lập trình

We have a collection in MongoDB, where _id is String uuid refering to some external entity.

Then there’s a process which select a list of records (based on time, whatever), process them and then attempts to remove the records.

   // "results" contains a list of records previously fetched from the db colleaction

   const deleteResults = await Promise.all(results
       .map(result => removeExpiredPayload(result._id, collection)))
        .catch(e => { throw e });
   console.log(deleteResults);

  const removeExpiredPayload = (_id, collection) => {
    console.log(`Removing event _id "${_id}"`)
    return collection.deleteOne({ _id });
  }

Yet – there are occasional (and not rare) cases, where MongoDB returns deletedCount as 0, even the record was found and processed. And the record stays in the DB collection.

logs:

Removing event _id "10f94444-fb52-4196-9b2f-802408312345"
Removing event _id "c3d45555-de06-4bee-b8bb-de690b543210"

[
  { acknowledged: true, deletedCount: 1 },
  { acknowledged: true, deletedCount: 0 }
]

Can MongoDB just ignore or refuse the delete request. Any idea why and what can be done about it?

LEAVE A COMMENT