What is the difference between findByIdAndRemove and findByIdAndDelete in Mongoose?

Asked

Viewed 1,150 times

5

I’m looking for the difference between the two and I can’t find it. They’re synonymous?

inserir a descrição da imagem aqui

1 answer

7


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

References:

  1. https://stackoverflow.com/questions/50602037/difference-between-findoneanddelete-and-findoneandremove
  2. https://mongoosejs.com/docs/api.html#model_Model.findByIdAndDelete
  3. https://mongoosejs.com/docs/api.html#model_Model.findOneAndDelete
  4. https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findAndModify
  • TL;DR = "Too long; didn’t read." -> Used to give a summary of a post that is very large. Not the case!

  • Four sentences is longer than one sentence. Yes, that’s the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.