DeprecationWarning:不建议使用collection.findAndModify.改用findOneAndUpdate,findOneAndReplace或findOneAndDelete吗? [英] DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead?

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

问题描述

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

I am using mongoose findOneAndUpdate but still getting the error,

DeprecationWarning:不建议使用collection.findAndModify.改用findOneAndUpdate,findOneAndReplace或findOneAndDelete.

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

但是我什至没有使用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.

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

'useFindAndModify':默认为true.设为false使 findOneAndUpdate()和findOneAndRemove()使用本机 findOneAndUpdate()而不是findAndModify().

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

,如果您看到猫鼬的定义文件,则提到它调用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的findOneAndUpdate()早于MongoDB驱动程序的 findOneAndUpdate()函数,因此它使用MongoDB驱动程序的 而是使用findAndModify()函数.

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

可以通过三种或更多种方式避免使用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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆