MongoDB mongoose collection.find选项弃用警告

时间:2018-08-19 10:14:16

标签: javascript node.js mongodb mongoose nosql

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

8 个答案:

答案 0 :(得分:49)

更新:

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选项已弃用,并由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*猫鼬写方法都使用mongodb本机驱动程序中已弃用的findAndModify方法。

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

对于removeupdate,分别将这些调用替换为delete*update*方法。

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

答案 1 :(得分:7)

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

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

答案 2 :(得分:3)

升级到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 :(得分:2)

您可以执行npm install mongoose@5.2.8,这将帮助您恢复到不会显示任何弃用警告的早期版本

答案 4 :(得分:1)

我目前正在使用“mongoose@5.11.15” 你可以通过使用这个来摆脱 DeprecationWarning,

方法一

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

方法二

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

答案 5 :(得分:0)

这在2020年4月对我有用:

.img-responsive {
    max-width: 30%;
    height: auto;
    display: block;
}

答案 6 :(得分:0)

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

答案 7 :(得分:0)

在连接数据库时只需传递以下选项

例如

const mongoose = require("mongoose");

mongoose.connect("uri",{ 
                       "useNewUrlParser": true,
                       "useUnifiedTopology": true, 
                       "useCreateIndex": true 
                       // other deprecations 
                       },(err)=>{
     // connection logging

});