MongoDB猫鼬弃用警告 [英] MongoDB mongoose Deprecation Warning

查看:113
本文介绍了MongoDB猫鼬弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 collection.find 查询文档时,我开始在控制台中收到以下警告

While querying the documents by using collection.find I started getting following warning in my console

DeprecationWarning:不建议使用collection.find选项[fields],将在更高版本中删除

DeprecationWarning: collection.find option [fields] is deprecated and will be removed in a later version

为什么我会看到此消息以及如何解决此问题?(可能的选择)

Why am I seeing this and how do I fix this? (Possible alternatives)

已添加查询

Session
        .find({ sessionCode: '18JANMON', completed: false })
        .limit(10)
        .sort({time: 1})
        .select({time: 1, sessionCode: 1});

猫鼬版本5.2.9

Mongoose version 5.2.9

推荐答案

更新:

5.2.10已发布并可以在此处下载.

5.2.10 is released and available for download here.

有关您可以查看的文档的更多信息页面 https://mongoosejs.com/docs/deprecations

For more info on the docs you can view page https://mongoosejs.com/docs/deprecations

有关此问题及其修复的更多信息 https://github.com/Automattic/mongoose/issues/6880

For more info on the issue and its fix https://github.com/Automattic/mongoose/issues/6880

原始答案:

Mongoose 5.2.9版本将本机mongodb驱动程序升级到3.1.3,其中添加了更改,以在调用不赞成使用的本机驱动程序方法时引发警告消息.

Mongoose 5.2.9 version upgraded the native mongodb driver to 3.1.3 in which changes were added to throw warning messages when the deprecated native driver method is called.

字段 选项已弃用,并由 projection 选项替换.

您将不得不等待猫鼬在其末端进行更改,以将场选项替换为投影.该修复程序计划于5.2.10发布.

You will have to wait for mongoose to make changes at their end to replace the fields option with projection. The fix is scheduled for 5.2.10 release.

暂时可以返回5.2.8,它将取消所有弃用警告.

For time being you can go back to 5.2.8 which will suppress all deprecation warnings.

npm install mongoose@5.2.8

对于所有其他不建议使用的警告,您必须视情况而定.

For all other deprecated warnings you have to approach them case by case.

使用其他收集方法时,还会看到其他弃用警告.

You will see other deprecation warnings when you use other collection methods.

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
DeprecationWarning: collection.save is deprecated. Use insertOne, insertMany, updateOne, or updateMany instead.
DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

默认情况下,所有 findOne * 猫鼬写方法都使用

All findOne* mongoose write methods by default use the findAndModify method which is deprecated in mongodb native driver.

使用 mongoose.set('useFindAndModify',false); 使mongooose在mongodb本机驱动程序上调用相应的 findOne * 方法.

Use mongoose.set('useFindAndModify', false); to have mongooose call the appropriate findOne* method on the mongodb native driver.

对于 remove update ,分别将这些调用替换为 delete * update * 方法.

For remove and update replace those calls with delete* and update* methods respectively.

对于 save ,分别将这些调用替换为 insert * / update * 方法.

For save replace those calls with insert*/ update* methods respectively.

使用 mongoose.set('useCreateIndex',true); 使mongooose在mongodb本机驱动程序上调用 createIndex 方法.

Use mongoose.set('useCreateIndex', true); to have mongooose call the createIndex method on the mongodb native driver.

这篇关于MongoDB猫鼬弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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