MongoDB mongoose 弃用警告 [英] 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});
Mongoose 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
选项.
您将不得不等待 mongoose 在其末尾进行更改以将字段选项替换为投影.该修复计划于 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*
mongoose 写入方法默认使用 findAndModify
方法,在 mongodb 本机驱动程序中已弃用.
All findOne*
mongoose write methods by default use the findAndModify
method which is deprecated in mongodb native driver.
使用 mongoose.set('useFindAndModify', false);
让 mongoose 在 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);
让mongoose调用mongodb原生驱动上的createIndex
方法.
Use mongoose.set('useCreateIndex', true);
to have mongooose call the createIndex
method on the mongodb native driver.
这篇关于MongoDB mongoose 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!