MongoDB mongoose collection.find选项弃用警告

问题描述

5.2.10已发布,可在此处下载。

使用mongoose.set('useCreateIndex', true);有mongooose调用createIndex上MongoDB的本地驱动程序的方法

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

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

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

fieldsoption已弃用,并由projectionoption 代替。

您将不得不等待猫鼬在其末端进行更改,以将场选项替换为投影。该修复程序计划于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本机驱动程序中已弃用的方法

用于mongoose.set('useFindAndModify', false);使mongooosefindOne*在mongodb本机驱动程序上调用适当的方法

对于remove和分别updatedelete*update*方法替换这些调用

用于分别saveinsert*/ update*方法替换这些调用

解决方法

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

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

为什么会看到此错误以及如何解决?(可能的替代方法)

编辑:添加查询

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

猫鼬版本5.2.9