弃用警告:collection.findAndModify 已弃用。使用 findOneAndUpdate、findOneAndReplace 或 findOneAndDelete 代替?

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

我正在使用 mongoose findOneAndUpdate 但仍然出现错误,

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

但我什至没有使用 findAndModify,为什么它将我的查询转换为 findAndModify

您需要将查询 useFindAndModify 中的选项设置为 false,如 docs.

中所述

(搜索关键字目前支持的选项有)

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

如果你看到 mongoose 的定义文件,其中提到它调用 findAndModify 更新命令。

 /**
  * 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>;

最近在 mongoose 文档 (Click here) 中更新了这些弃用,其中提到:

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

您可以通过三种或更多方式避免使用 FindAndModify:

  1. 在全局级别:将选项设置为 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. 在连接级别:我们可以使用连接选项进行配置:
    mongoose.connect(uri, { useFindAndModify: false });
  1. 在查询级别:
   await ModelName.findOneAndUpdate({matchQuery},
   {$set: updateData}, {useFindAndModify: false});

像这样全局更改 mongoose 配置:

mongoose.set('useFindAndModify', false);

或者像这样在查询字符串中传递选项:

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

您还可以管理其他猫鼬弃用警告 docs

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

就是这样。

您也可以在连接时通过要求选项 useNewUrlParser 传递选项。看下面->https://mongoosejs.com/docs/deprecations.html

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

您必须更改连接方法选项才能消除错误:

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

你可以这样使用它。

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

跳过所有错误:-) 将其添加到您的 index.js 或任何您命名的名称中。我的意思是主要的js文件。 ;-)

猫鼬版本更新如此之多,

对于使用 Model.findByIdAndUpdate() 它需要一个选项参数也见下文

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

为了解决这个问题

在开始的mongoose.connect中传递这个useFindAndModify: false

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

mongoose.set('useFindAndModify', false); 

clickhere 检查相关弃用

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

这些解决方案对我有用!

要修复所有弃用警告,请按照以下步骤操作:

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

将 update() 替换为 updateOne()、updateMany() 或 replaceOne() 将 remove() 替换为 deleteOne() 或 deleteMany()。 将 count() 替换为 countDocuments(),除非您想计算整个集合中有多少文档(无过滤器)。在后一种情况下,使用 estimatedDocumentCount().