弃用警告:不推荐使用 collection.findAndModify.改用 findOneAndUpdate、findOneAndReplace 或 findOneAndDelete? [英] DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead?

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

问题描述

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

I am using mongoose findOneAndUpdate but still getting the error,

弃用警告:不推荐使用 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':默认为真.设置为 false 使findOneAndUpdate() 和 findOneAndRemove() 使用原生findOneAndUpdate() 而不是 findAndModify().

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

如果你看到 mongoose 的定义文件,那里提到它调用 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>;

最近在 mongoose 文档(单击此处)中更新了这些弃用提到:

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});

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

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