javascript – MongoDB mongoose collection.find选项弃用警告

在使用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});

猫鼬版本5.2.9

解决方法:

更新:

5.2.10已发布并可供下载here.

使用mongoose.set(‘useCreateIndex’,true);让mongooose在mongodb本机驱动程序上调用createIndex方法.

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

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

原答案:

Mongoose 5.2.9版本将本机mongodb驱动程序升级到3.1.3,其中添加了更改以在调用不推荐使用的本机驱动程序方法时抛出警告消息.

不推荐使用fields选项,并将其替换为投影选项.

您将不得不等待mongoose在其末尾进行更改以使用投影替换fields选项.该修复程序计划在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写入方法都使用mongodb本机驱动程序中不推荐使用的findAndModify方法.

使用mongoose.set(‘useFindAndModify’,false);让mongooose在mongodb本机驱动程序上调用相应的findOne *方法.

对于删除和更新,分别用delete *和update *方法替换这些调用.

对于save,分别使用insert * / update *方法替换这些调用.

相关文章

校园网上订餐系统系统主要功能模块包括公告内容(公告栏、轮...
从今天开始,正式进入项目阶段。本次的项目是跟着黑马的瑞吉...
在可预见的将来,IT不会萎缩,只会越发展越庞大,最终会渗透...
vulntarget-b靶场最详细通关记录。
MongoDB是一个开源、高性能、支持海量数据存储的。
【NoSQL数据库技术与应用】课本代码、课后答案(持续更新)