在使用 collection.find
查询文档时,我开始在控制台中收到以下警告
DeprecationWarning: collection.find option [fields] is deprecated and will be removed in a later version
为什么我会看到这个,我该如何解决这个问题? (可能的替代方案)
编辑:添加查询
Session
.find({ sessionCode: '18JANMON', completed: false })
.limit(10)
.sort({time: 1})
.select({time: 1, sessionCode: 1});
Mongoose 版本 5.2.9
最佳答案
更新:
5.2.10 发布并可供下载here .
有关文档的更多信息,您可以查看页面 https://mongoosejs.com/docs/deprecations
有关此问题及其修复的更多信息 https://github.com/Automattic/mongoose/issues/6880
原答案:
Mongoose 5.2.9 版本将 native mongodb 驱动程序升级到 3.1.3,其中添加了更改以在调用已弃用的 native 驱动程序方法时抛出警告消息。
fields
选项已弃用,并替换为 projection
选项。
您将不得不等待 mongoose 在其末尾进行更改以将字段选项替换为投影。该修复计划在 5.2.10 版本中发布。
暂时您可以返回到 5.2.8,这将禁止所有弃用警告。
npm install mongoose@5.2.8
对于所有其他已弃用的警告,您必须逐个处理它们。
当您使用其他收集方法时,您会看到其他弃用警告。
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 native 驱动程序中已弃用的方法。
使用 mongoose.set('useFindAndModify', false);
让 mongooose 在 mongodb 原生驱动程序上调用相应的 findOne*
方法。
对于 remove
和 update
分别用 delete*
和 update*
方法替换这些调用。
对于 save
将这些调用分别替换为 insert*
/update*
方法。
使用 mongoose.set('useCreateIndex', true);
让 mongooose 在 mongodb 原生驱动上调用 createIndex
方法。
https://stackoverflow.com/questions/51916630/