0

Since that I update my mongoose package, I see this message.

DeprecationWarning: Mongoose: findOneAndUpdate() and findOneAndDelete() without the useFindAndModify option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

Oh my God !!! I use this method everything....

https://mongoosejs.com/docs/deprecations.html

After reading the documentation, I understand that the mongoose method use the Mongo Driver's findOneAndUpdate and now, we should be use Mongo Drive's findAndModify.

So I have two possibilities...

  1. I do nothing and in the future, the warning will go away
  2. I set this code after my mongoose connection mongoose.set('useFindAndModify', false); , the warning disappears and everyone is happy...

But if I use useFindAndModify for the moment, ow do you know when it will become useless ?

What is the best pratice for you in this situation please ?

Thank you very much !!!

David
  • 21
  • 6
  • You have the options available in the docs ("Best?" no best here). One option is to change your code (after instead of before) and the other is to change configure (useFindAndModify: false). Your code will not break. Over time - i think, a cleaner solution is to update the code – Ezra Siton Dec 30 '20 at 12:20
  • Ok, thank you for your response. In this case, I don't find an equivalent method at findOneAndUpdate. findOneAndUpdate update the document AND return the new document. With updateOne and updateMany, the method return collection data but not the new document... Maybe it is this my real question haha – David Dec 30 '20 at 12:53
  • `updateOne`: Returns either the original document or, if `returnNewDocument: true`, the updated document. – Ezra Siton Dec 30 '20 at 14:07

1 Answers1

0

I would say the second option.

  1. I set this code after my mongoose connection mongoose.set('useFindAndModify', false);, the warning disappears and everyone is happy...

It seem findAndModify is deprecated and mongoose recommend the following options now: findOneAndUpdate, findOneAndReplace or findOneAndDelete.

I was researching the difference between findOneAndDelete and findOneAndRemove when I stumbled onto something similar. It seems findOneAndRemove uses findAndModify under the hood, and mongoose is deprecating this so findOneAndDelete is now recommended.

You can check this out: What is the difference between findByIdAndRemove and findByIdAndDelete in mongoose?

Tyler2P
  • 2,324
  • 26
  • 22
  • 31