MongoDB猫鼬弃用警告 [英] MongoDB mongoose Deprecation Warning
问题描述
使用 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.
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屋!