本文介绍了DeprecationWarning:不建议使用collection.findAndModify.改用findOneAndUpdate,findOneAndReplace或findOneAndDelete吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用猫鼬findOneAndUpdate,但仍然收到错误消息,

I am using mongoose findOneAndUpdate but still getting the error,

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

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

推荐答案

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

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

(搜索关键字当前支持的选项是)

,如果您看到猫鼬的定义文件,则提到它调用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:

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

There are three ways or more by which you can avoid the use of 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});

这篇关于DeprecationWarning:不建议使用collection.findAndModify.改用findOneAndUpdate,findOneAndReplace或findOneAndDelete吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 17:16