MongoDB mongoose 弃用警告

44

当我使用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


嗨,@DanieleTassone,每当我使用find()时,就会出现这个警告。 - It worked yesterday.
1
原生 MongoDB 驱动程序是 Mongoose 内部用于处理 MongoDB 的东西。如果 Mongoose 不遵守一些新的“规则”,那么会返回警告。例如,如果使用“fields 选项”而不是游标函数,则将因原生驱动程序而收到此警告。请看这里:https://github.com/Automattic/mongoose/issues/6667。 - Daniele Tassone
1
github 上有一个问题被提出了... 所以在它得到解决之前,您可以使用 mongoose 版本 5.2.8 - Ashh
如果你需要降级到5.2.8版本,请告诉我,npm install mongoose@5.2.8 应该可以解决问题。修复已经合并到主分支中了,详情请见 fix。因此,当5.2.10版本发布时,应该已经包含了这个修复。 - s7vr
谢谢 @Veeram :) - It worked yesterday.
显示剩余2条评论
14个回答

72

更新:

Mongoose 5.2.10 版本已发布并可在此处下载。

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

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

原始回答:

Mongoose 5.2.9 版本将本地 mongodb 驱动程序升级到 3.1.3 版本,其中添加了警告消息以在调用已弃用的本地驱动程序方法时抛出。

fields选项已过时,并已替换为projection选项。

您需要等待mongoose进行更改以将fields选项替换为projection。该修复计划在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在mongodb原生驱动上调用合适的findOne*方法,使用 mongoose.set('useFindAndModify', false);

removeupdate 建议分别使用 delete*update* 方法。

save 建议分别使用 insert* / update* 方法。

使用 mongoose.set('useCreateIndex', true); 来让mongoose在mongodb原生驱动上调用createIndex方法。


我有点困惑:为什么你在这个问题是关于已弃用的“find”选项时,更新了答案并提供了如何修复“ensureIndex”弃用的信息? - Jelle De Loecker
@skerit 谢谢 - 已更新答案。我试图覆盖所有已弃用的警告。 - s7vr

18
mongoose.connect('your db url', {
  useCreateIndex: true,
  useNewUrlParser: true
})
或者
mongoose.set('useCreateIndex', true)
mongoose.connect('your db url', { useNewUrlParser: true })

2
虽然这可能回答了作者的问题,但它缺少一些解释性的词语和/或文档链接。裸代码片段没有周围的一些短语是不太有帮助的。您也可以在如何撰写一个好的答案中找到很多帮助。请编辑您的答案。 - Roy Scheffers
那个是个很好的答案。警告已经消失了。 - search-learn

5

升级到版本5.2.10后,可以使用以下任何选项:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', {

  useCreateIndex: true,
  useNewUrlParser: true

})
.then(() => console.log('connecting to database successful'))
.catch(err => console.error('could not connect to mongo DB', err));



const mongoose = require('mongoose');

mongoose.set('useCreateIndex', true);

mongoose.connect('mongodb://localhost/test',{

    useNewUrlParser: true

})
.then(() =>  console.log('connecting to database successful') )
.catch(err =>  console.error('could not connect to mongo DB', err) );

3

你可以运行 npm install mongoose@5.2.8 命令来安装一个较早版本的 Mongoose,这样就不会显示任何废弃警告了。


只是为了避免警告而降低版本吗? - CodeFinity

1
我目前正在使用"mongoose@5.11.15",您可以通过以下方法消除DeprecationWarning: 方法1:
mongoose.connect("Your DB address", {
useNewUrlParser: true,                       
useUnifiedTopology: true,                 
useCreateIndex: true               // to handle collection.ensureIndex is deprecated
});

Method 2

mongoose.connect("Your DB address", {
useNewUrlParser: true,                       
useUnifiedTopology: true,                 // other deprecation warnings            
});
mongoose.set("useCreateIndex", true);     // to handle collection.ensureIndex is deprecated

1

当我们发出连接请求以连接MongoDB时,只需在回调函数中添加一个键值对即可。

useCreateIndex:true

在函数内部

mongoose.connect(config.DB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex:true
}

1

1

mongoose.connect('mongodb://localhost:27017/tablename',{ useUnifiedTopology: true, useNewUrlParser: true,useCreateIndex: true },()=>{ console.log(已连接数据库) });


1
这是2020年4月份我使用的方法:

mongoose.connect(process.env.DATABASE_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
})

0

我正在使用mongoose版本6.8.0,这对我起作用了

 //connect mongodb 
     mongoose.set('strictQuery', false);
     mongoose.connect('url',(err,db) => {

    if(err){
        console.log(`Database not connected :   ${err}`)
    }else{
        console.log('Database connected Successfully')
    }
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接