MongoDB mongoose collection.find选项弃用警告 [英] MongoDB mongoose collection.find options 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
Mongoose version 5.2.9
推荐答案
更新:
5.2.10已发布并可供下载此处。
5.2.10 is released and available for download here.
使用 mongoose.set('useCreateIndex',true);
让mongooose在mongodb上调用 createIndex
方法本地驱动程序。
Use mongoose.set('useCreateIndex', true);
to have mongooose call the createIndex
method on the mongodb native driver.
有关文档的更多信息,请查看页面
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.
fields
选项已弃用,并替换为投影
选项。
您必须等待mongoose在其末尾进行更改才能用投影替换fields选项。该修复程序计划在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 $ c在mongodb本机驱动程序中弃用的$ c>
方法。
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.
For 删除
和更新
用删除*
和<替换这些调用code>更新* 方法。
For remove
and update
replace those calls with delete*
and update*
methods respectively.
对于保存
替换这些来电分别用 insert *
/ 更新*
方法。
For save
replace those calls with insert*
/ update*
methods respectively.
这篇关于MongoDB mongoose collection.find选项弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!