72

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?

Katie
  • 1,498
  • 1
  • 15
  • 33

7 Answers7

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

冉娃娃
  • 550
  • 5
  • 7
22

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
xirururu
  • 5,028
  • 9
  • 35
  • 64
  • 8
    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 them – Katie Jun 07 '21 at 19:57
  • 1
    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. @Katie – xirururu Jun 08 '21 at 13:12
  • 1
    @xirururu 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. – Amir Asyraf Jan 12 '22 at 14:39
  • @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! – xirururu Jan 15 '22 at 03:43
  • @xirururu 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. – Amir Asyraf Jan 21 '22 at 02:04
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.

Abdulhakim
  • 620
  • 8
  • 11
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.
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.

Ritik
  • 51
  • 7
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

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

Yogesh C
  • 1
  • 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. – ksugiarto Jul 29 '22 at 19:06