What is the difference between findByIdAndRemove and findByIdAndDelete in mongoose?

Mongoose

Mongoose Problem Overview


The documentation here doesn't provide much of an explanation for why there are two different operations to accomplish the same thing, so I'm wondering what the differences are between them. Why might I choose to use one over the other?

Mongoose Solutions


Solution 1 - Mongoose

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.

the offical site https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete

Solution 2 - Mongoose

There is no difference between them! ^_^

Let's look into the code, at the findByIdAndDelete(), there is a note:

// Note: same signatures as findByIdAndRemove

and by findByIdAndRemove() the same:

// Note: same signatures as findByIdAndDelete

Solution 3 - Mongoose

MongoDB is updating its methods, like any other coding language or program. As you can see here : https://mongoosejs.com/docs/deprecations.html

remove() and findOneAndRemove() has been deprecated in favour of deleteOne() and deleteMany().

I guess findByIdAndRemove() is not deprecated yet, but probably it will also be deprecated for transition to delete only methods.

Solution 4 - Mongoose

There is no difference between remark of them.

Both functions return the found document if any.

// Finds a matching document, removes it, passing the found document (if any) to the callback.

Solution 5 - Mongoose

Issue a MongoDB findOneAndDelete() command by a document's _id field. In other words, findByIdAndDelete(id) is a shorthand for findOneAndDelete({ _id: id }).

Issue a mongodb findAndModify remove command by a document's _id field. findByIdAndRemove(id, ...) is equivalent to findOneAndRemove({ _id: id }, ...).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKatieView Question on Stackoverflow
Solution 1 - Mongoose冉娃娃View Answer on Stackoverflow
Solution 2 - MongoosexirururuView Answer on Stackoverflow
Solution 3 - MongooseAbdulhakimView Answer on Stackoverflow
Solution 4 - Mongooseidam achmad FaizinView Answer on Stackoverflow
Solution 5 - MongooseYogesh CView Answer on Stackoverflow