g1ji
g1ji

Reputation: 1129

Difference between findOneAndDelete() and findOneAndRemove()

I am not able to differentiate findOneAndDelete() And findOneAndRemove() in the mongoose documentaion.

Query.prototype.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.

Upvotes: 56

Views: 33115

Answers (8)

ZachB
ZachB

Reputation: 15386

The other answers here have a lot of wrong info. They ultimately send the exact same wire command to the MongoDB server. You should just use findOneAndDelete(). In more detail:

Mongoose's Model.findOneAndRemove(query) becomes MongoDB's findAndModify({query, remove: true}).

Mongoose's Model.findOneAndDelete() becomes MongoDB's findOneAndDelete(). However, the MongoDB driver converts findOneAndDelete(query, opts) to findAndModify({query, remove: true}) (src). Thus they are the exact same thing.

Both take the same options.

Both return the deleted document.

Upvotes: 2

Fouad Boukredine
Fouad Boukredine

Reputation: 1633

Here is the exact difference (quoted from the mongoose docs in Model.findOneAndDelete() section):

"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."

Here is the link to it: https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete

Upvotes: 1

kochauf
kochauf

Reputation: 356

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

Upvotes: 14

Manni Dula
Manni Dula

Reputation: 49

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.

Upvotes: 4

Moad Ennagi
Moad Ennagi

Reputation: 1073

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)

Upvotes: 4

Vivek  Ghanchi
Vivek Ghanchi

Reputation: 232

findOneAndRemove returns the removed document so if you remove a document that you later decide should not be removed, you can insert it back into the db. Ensuring your logic is sound before removing the document would be preferred to checks afterward IMO.

findOneAndDelete has the sort parameter which can be used to influence which document is updated. It also has a TimeLimit parameter which can control within which operation has to complete

Upvotes: 0

dotDone
dotDone

Reputation: 143

I would suggest you use findOneAndDelete(). Mongoose provides both the features to handle data using the ORM and also features to write directly into the database, with findOneAndDelete() being one of the latter. Writing to database directly is more dangerous as you run the risk of not calling middleware or validators, potentially submitting partial or incomplete data to the database. Note that I said it's more dangerous, not that it's flat out dangerous, findOneAndDelete() just goes throughout the ORM adding safety.

Upvotes: -1

Xcode
Xcode

Reputation: 129

In mongoose findOneAndDelete works the same way as findOneAndRemove. They both look an object by its properties in JSON, then goes ahead to delete it and as well as return it object once after deletion. When you are using the native mongodb as your database findOneAndDelete might be useful to you, but in the case of mongoose it is deprecated I may advise you to use findOneAndDelete to perform your operation based on the latest mongoose and nodejs configuration as at this period. https://github.com/Automattic/mongoose/issues/6880

Upvotes: 1

Related Questions