Javascript MongoDB猫鼬弃用警告
在使用collection.find查询文档时,我开始在控制台中收到以下警告 弃用警告:collection.find选项[fields]已弃用,并且 将在以后的版本中删除 为什么我会看到这一点?我该如何解决这一问题?(可能的替代方案) 编辑:添加查询Javascript MongoDB猫鼬弃用警告,javascript,node.js,mongodb,mongoose,nosql,Javascript,Node.js,Mongodb,Mongoose,Nosql,在使用collection.find查询文档时,我开始在控制台中收到以下警告 弃用警告:collection.find选项[fields]已弃用,并且 将在以后的版本中删除 为什么我会看到这一点?我该如何解决这一问题?(可能的替代方案) 编辑:添加查询 Session .find({ sessionCode: '18JANMON', completed: false }) .limit(10) .sort({time: 1}) .s
Session
.find({ sessionCode: '18JANMON', completed: false })
.limit(10)
.sort({time: 1})
.select({time: 1, sessionCode: 1});
猫鼬版本5.2.9更新: 5.2.10已发布,可供下载 有关文档的更多信息,请查看页面 有关此问题及其修复的更多信息 原始答案: Mongoose 5.2.9版本将本机mongodb驱动程序升级到3.1.3,其中添加了更改,以在调用不推荐的本机驱动程序方法时抛出警告消息 选项已弃用,并替换为
投影
选项
您将不得不等待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本机驱动程序中不推荐的方法
使用mongoose.set('useFindAndModify',false)
让Mongoose在mongodb本机驱动程序上调用相应的findOne*
方法
对于remove
和update
分别用delete*
和update*
方法替换这些调用
对于save
分别用insert*
/update*
方法替换这些调用
使用
mongoose.set('useCreateIndex',true)
让Mongoose调用mongodb本机驱动程序上的createIndex
方法。您可以执行npm安装mongoose@5.2.8
,这将帮助您返回到升级到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) );
或
这在2020年4月对我有效:
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
mongoose.connect('mongodb://localhost:27017/tablename“,{useUnifiedTopology:true,useNewUrlParser:true,useCreateIndex:true},()=>{
console.log(
connected db)
});代码>在数据库连接时只需传递以下选项
例如
const mongoose = require("mongoose");
mongoose.connect("uri",{
"useNewUrlParser": true,
"useUnifiedTopology": true,
"useCreateIndex": true
// other deprecations
},(err)=>{
// connection logging
})) 我目前正在使用“mongoose@5.11.15"
您可以通过使用此
方法1
mongoose.connect("Your DB address", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true // to handle collection.ensureIndex is deprecated
});
方法2
mongoose.connect("Your DB address", {
useNewUrlParser: true,
useUnifiedTopology: true, // other deprecation warnings
});
mongoose.set("useCreateIndex", true); // to handle collection.ensureIndex is deprecated
您是否尝试使用collection.find(query).limit(1).project({name:1})?Hi@danieletasone每当我使用find()时都会出现此警告您是否可以发布完整的查询本机mongodb驱动程序是Mongoose内部用来处理mongodb的。如果猫鼬不遵守一些新的“规则”,则会返回警告。例如,使用本机驱动程序时,如果使用“字段选项”而不是光标函数,则会收到此警告。在这里看一看:有一个问题是开放的。。。因此,在问题解决之前,您可以使用mongoose版本5.2.8。虽然这可能回答了作者的问题,但它缺少一些解释性的词语和/或文档链接。如果没有一些短语,原始代码片段就没有多大帮助。你也会发现这很有帮助。请编辑您的答案。我很困惑:当这个问题是关于弃用的find
选项时,为什么您要用有关如何修复ensureIndex
弃用的信息更新答案?@skerit谢谢-更新答案。我试图涵盖所有不推荐的警告。
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