7

Using mongodb-4.0.10 and mongoose-5.2.10

Added useFindAndModify: false to the mongoose configuration to avoid warnings like:

DeprecationWarning: Mongoose: findOneAndUpdate() and findOneAndDelete() without the useFindAndModify option set to false are deprecated.

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

But now 'the options [useFindAndModify] is not supported' is coming on running the app.

ambianBeing
  • 3,449
  • 2
  • 14
  • 25
Bilal Khan
  • 279
  • 1
  • 3
  • 14
  • 1
    Where are you setting the `useFindAndModify : false`. It should be either at the global `mongoose` level `mongoose.set('useFindAndModify', false);` or can be passed to connection `mongoose.connect(uri, { useFindAndModify: false });` – ambianBeing Jan 02 '20 at 08:17
  • @ambianBeing I am passing the option inside mongoose.connect. – Bilal Khan Jan 02 '20 at 10:18

2 Answers2

11

For others who are facing this issue, useFindAnyModify is not supported if you are using mongoose version 6+.

To solve this either we can remove useFindAnyModify or downgrade mongoose version to use v5+.

Kunal Tyagi
  • 2,341
  • 1
  • 15
  • 26
  • 1
    Just adding a reference that supports this answer, from the official documentation for mongoose version 6+ https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options – Abdul Aug 03 '23 at 03:14
1

First, update mongoose to ^latest (5.8.X) version.

Then rewrite you code and try to use it like this

mongoose.connect(`mongodb+srv://${login}:${password}@${hostname}/${auth_db}`, {
    useNewUrlParser: true,
    useFindAndModify: false,
    retryWrites: true,
    w: "majority",
});

mongoose.Promise = global.Promise;

Also, according to mongoose docs on findOneAndUpdate you could use findOne and update docs via triggering .save on result.

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64