Difference between findOneAndDelete() and findOneAndRemove()

TLDR: You should use findOneAndDelete() unless you have a good reason not to.


Longer answer:

From the Mongoose documentation of findOneAndDelete:

This function differs slightly from Model.findOneAndRemove() in that findOneAndRemove() becomes a MongoDB findAndModify() command, as opposed to a findOneAndDelete() command. For most mongoose use cases, this distinction is purely pedantic. You should use findOneAndDelete() unless you have a good reason not to.

Mongoose Docs findOneAndDelete


In MongoDB native client (Mongoose is different) passing { remove: true } to findAndModify makes it remove the found document, it's behavior in this case would appear quite similar to findOneAndDelete. However, there are some differences when using other options:

  • findAndModify takes 5 parameters :query, sort, doc (the update object), options and a callback. findOneAndDelete only takes 3 (filter or query, options and a callback)
  • options for findAndModify include w, wtimeout and j for write concerns, as:

    “the level of acknowledgment requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters.“ or simply guarantee levels available for reporting the success of a write operation.

    options for findOneAndDelete do not include write concerns configuration.

  • findAndModify lets you return the new document and remove it at the same time.

Example:

// Simple findAndModify command returning the new document and
  // removing it at the same time
  collection.findAndModify({b:1}, [['b', 1]], {$set:{deleted: Date()}}, {remove:true}, calback)

Both of them are almost similar except findOneAndRemove uses findAndModify with remove flag and time complexity will be a bit higher compare to findOneAndDelete because you are doing an update. Delete are always faster.