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

node.jsMongodbMongooseDeprecated

node.js Problem Overview


I am using mongoose findOneAndUpdate but still getting the error,

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

But I am not even using findAndModify, why is it converting my query to findAndModify?

node.js Solutions


Solution 1 - node.js

You need to set the option in the query useFindAndModify to false, as mentioned in the docs.

(search keyword Currently supported options are)

> 'useFindAndModify': true by default. Set to false to make > findOneAndUpdate() and findOneAndRemove() use native > findOneAndUpdate() rather than findAndModify().

and if you see the definition file of mongoose, where mentioned that it calls findAndModify update command.

 /**
  * Issues a mongodb findAndModify update command.
  * Finds a matching document, updates it according to the update arg, 
    passing any options,
  * and returns the found document (if any) to the callback. The query 
    executes immediately
  * if callback is passed else a Query object is returned.
  */
 findOneAndUpdate(): DocumentQuery<T | null, T>;

Recently updated in the mongoose docs (Click here) for these deprecation where mentioned:

> Mongoose's findOneAndUpdate() long pre-dates the MongoDB driver's > findOneAndUpdate() function, so it uses the MongoDB driver's > findAndModify() function instead.

There are three ways or more by which you can avoid the use of FindAndModify:

  1. At Global level: Set the option to false.
// Make Mongoose use `findOneAndUpdate()`. Note that this option is `true`
// by default, you need to set it to false.
mongoose.set('useFindAndModify', false);
  1. At connection level: we can configure using the connection options:
    mongoose.connect(uri, { useFindAndModify: false });
  1. At Query level:
   await ModelName.findOneAndUpdate({matchQuery},
   {$set: updateData}, {useFindAndModify: false});

Solution 2 - node.js

Change the mongoose configuration globally like this:

mongoose.set('useFindAndModify', false);

Or pass the options in your query string like this:

Person.findOneAndUpdate({_id: id}, {$set: body}, {new: true, useFindAndModify: false}).then(..

You can also manage the other mongoose deprecation warnings as mention https://mongoosejs.com/docs/deprecations.html">docs</a>

mongoose.set('useNewUrlParser', true);
mongoose.set('useCreateIndex', true);

That's it.

Solution 3 - node.js

you also can pass the options at the connection with the requirement option useNewUrlParser. Look at the following -> https://mongoosejs.com/docs/deprecations.html

mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useFindAndModify: false}); 

Solution 4 - node.js

You have to change your connect method options to get rid of the error:

mongoose.connect("mongodb://localhost/DB_Name", {
  keepAlive: true,
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false
});

You can use it like this.

Solution 5 - node.js

Mongoose version updates so much that,

for using Model.findByIdAndUpdate() it takes an option parameter also see below

List.findByIdAndUpdate(id, update, options, callback) // executes

for solving this

Pass this useFindAndModify: false in the mongoose.connect in the starting

mongoose.connect("mongodb://localhost:27017/yourDatabase", { useNewUrlParser: true, useUnifiedTopology: true ,useFindAndModify: false });

or

mongoose.set('useFindAndModify', false); 

clickhere to check related deprecations

Solution 6 - node.js

  mongoose.set('useNewUrlParser', true);
  mongoose.set('useFindAndModify', false);
  mongoose.set('useCreateIndex', true);
  mongoose.set('useUnifiedTopology', true);

These solutions work for me!

Solution 7 - node.js

Mongoose.connect(Config.database,{useUnifiedTopology: true,useNewUrlParser: true,useFindAndModify:false});

to skip all errors :-) add this on your index.js or whatever you named it. I mean main js file. ;-)

Solution 8 - node.js

To fix all deprecation warnings, follow the below steps:

mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);

Replace update() with updateOne(), updateMany(), or replaceOne() Replace remove() with deleteOne() or deleteMany(). Replace count() with countDocuments(), unless you want to count how many documents are in the whole collection (no filter). In the latter case, use estimatedDocumentCount().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSudhanshu GaurView Question on Stackoverflow
Solution 1 - node.jsShivam PandeyView Answer on Stackoverflow
Solution 2 - node.jsIkram - Ud - DaulaView Answer on Stackoverflow
Solution 3 - node.jsPollinyView Answer on Stackoverflow
Solution 4 - node.jsDeveshView Answer on Stackoverflow
Solution 5 - node.jsRaghu P RanganView Answer on Stackoverflow
Solution 6 - node.jsSounak RoyView Answer on Stackoverflow
Solution 7 - node.jsSujeewa K. AbeysingheView Answer on Stackoverflow
Solution 8 - node.jsMD SHAYONView Answer on Stackoverflow