What is the difference between findByIdAndRemove and findByIdAndDelete in mongoose?
Asked Answered
S

9

74

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?

Salaried answered 7/1, 2019 at 20:12 Comment(0)
S
50

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

Swamper answered 11/2, 2019 at 11:5 Comment(2)
What would count as a "good reason not to" use findOneAndDelete?Salaried
"You should use findOneAndDelete() unless you have a good reason not to" - answers my question. Thank you.Inurbane
H
23

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
Hilversum answered 27/2, 2020 at 23:33 Comment(5)
Having the same signature does not necessarily mean they do the same thing. For example, the following two functions have the same signature, but do very different things: const add = (a, b) => a + b; const multiply = (a, b) => a * b; They both have the same signature because they each take two numbers as arguments and return a number, however one is adding those numbers and the other is mulitplying themSalaried
Please go deep into the source code, not only the notation, at the time I posted the answer, the two functions have exactly the same implementation code. @SalariedHilversum
@Hilversum Yes, but their point was two functions having the same signature does not mean the functions do the same thing, as inferred by the answer.Proximal
@AmirAsyraf I mean the Javascript code Implementation are exactly the same , at least at the time I answered the question. Please go deep into the source code! If they are different, please post it and let us know!Hilversum
@Hilversum Look, I get that the code is/was the same. But your posted answer implies they're the same just because they have the same signature. e.g. // Note: same signatures as findByIdAndRemove. If you instead posted the code block instead or just mentioned the codes were exactly the same, there wouldn't have been any debate.Proximal
A
13

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.

Agrapha answered 12/6, 2021 at 17:56 Comment(0)
G
3

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.
Goff answered 12/10, 2020 at 18:34 Comment(0)
K
3

I got the reason findByIdAndRemove returns the deleted document & findByIdAndDelete does not return. If we want the deleted document then we can use findByIdAndRemove otherwise can use findByIdAndDelete.

Recommend:- If don't want to get the deleted document then have to use findByIdAndDelete because it's fast cause does not return the document.

Kareenkarel answered 23/4, 2023 at 12:6 Comment(0)
J
1

It appears as findByIdAndRemove() was deprecated (and breaks) with the transition from Mongoose 7.x to 8.x (released October 31, 2023). Although, I don't see the removal listed in https://mongoosejs.com/docs/migrating_to_8.html.

Jimmy answered 27/11, 2023 at 17:28 Comment(0)
C
0

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.

read more: docs

Cisterna answered 11/6, 2022 at 2:38 Comment(1)
did you just copy-paste another answer? :))Contort
C
0

findByIdAndRemove:

1.This method finds a single document by its ID and removes it. 2.It returns the removed document. 3.The findByIdAndRemove method has been deprecated in Mongoose starting from version 5.11.0 in favor of findByIdAndDelete.

findByIdAndDelete:

1.This method also finds a single document by its ID and removes it. 2.It returns the removed document. 3.findByIdAndDelete is the recommended method to use instead of findByIdAndRemove since the latter has been deprecated.

In summary, both methods perform the same operation, but findByIdAndRemove has been deprecated, and developers are encouraged to use findByIdAndDelete instead for consistency and to avoid potential issues in future Mongoose versions.

Cabrales answered 21/1 at 14:42 Comment(0)
S
-1

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 }, ...).

Sibley answered 30/4, 2022 at 13:54 Comment(1)
Dude, welcome to Stackoverflow, and you just copy-paste the exact thing from the documentation which we already know from the link on the question.Huckaback

© 2022 - 2024 — McMap. All rights reserved.