5
TL;DR - The two do the same thing in different ways, but it is recommended to use the findByIdAndDelete()
.
The small difference is that the findByIdAndDelete()
uses Mongodb’s native function findOneAndDelete()
to remove and the findByIdAndRemove()
uses Mongodb’s native function findAndModify()
to do the same thing. This function (findAndModify()
) was discontinued (deprecated) and the Mongoose documentation recommends using the findByIdAndDelete()
.
This Function differs Slightly from
Model.findOneAndRemove()
in thatfindOneAndRemove()
Becomes a MongodbfindAndModify()
command, as opposed to afindOneAndDelete()
command. For Most Mongoose use cases, this Distinction is Purely pedantic. You should usefindOneAndDelete()
unless you have a good Reason not to.
References:
- https://stackoverflow.com/questions/50602037/difference-between-findoneanddelete-and-findoneandremove
- https://mongoosejs.com/docs/api.html#model_Model.findByIdAndDelete
- https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete
- https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findAndModify
Hi...here
– Ernesto Casanova