node.js 不推荐使用服务器发现和监控引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57895175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server Discovery And Monitoring engine is deprecated
提问by iLiA
I am using Mongoose with my Node.js app and this is my configuration:
我在 Node.js 应用程序中使用 Mongoose,这是我的配置:
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
}).then(()=>{
console.log(`connection to database established`)
}).catch(err=>{
console.log(`db error ${err.message}`);
process.exit(-1)
})
but in the console it still gives me the warning:
但在控制台中它仍然给我警告:
DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
弃用警告:不推荐使用当前的服务器发现和监控引擎,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
What is the problem? I was not using useUnifiedTopologybefore but now it shows up in the console. I added it to the config but it still gives me this warning, why? I do not even use MongoClient.
问题是什么?我以前没有使用useUnifiedTopology过,但现在它显示在控制台中。我将它添加到配置中,但它仍然给我这个警告,为什么?我什至不使用MongoClient.
Edit
编辑
As Felipe Plets answered there was a problem in Mongoose and they fixed this bug in later versions. So you can solve problem by updating mongoose version.
正如 Felipe Plets 回答的那样,Mongoose 存在一个问题,他们在以后的版本中修复了这个错误。所以你可以通过更新猫鼬版本来解决问题。
回答by Felipe Plets
Update
更新
Mongoose 5.7.1 was release and seems to fix the issue, so setting up the useUnifiedTopologyoption work as expected.
Mongoose 5.7.1 已发布,似乎解决了该问题,因此设置useUnifiedTopology选项按预期工作。
mongoose.connect(mongoConnectionString, {useNewUrlParser: true, useUnifiedTopology: true});
Original answer
原答案
I was facing the same issue and decided to deep dive on Mongoose code: https://github.com/Automattic/mongoose/search?q=useUnifiedTopology&unscoped_q=useUnifiedTopology
我面临同样的问题并决定深入研究 Mongoose 代码:https: //github.com/Automattic/mongoose/search?q =useUnifiedTopology &unscoped_q =useUnifiedTopology
Seems to be an option added on version 5.7 of Mongoose and not well documented yet. I could not even find it mentioned in the library history https://github.com/Automattic/mongoose/blob/master/History.md
似乎是在 Mongoose 5.7 版中添加的一个选项,但还没有很好的文档记录。我什至找不到在图书馆历史https://github.com/Automattic/mongoose/blob/master/History.md 中提到的
According to a comment in the code:
根据代码中的注释:
- @param {Boolean} [options.useUnifiedTopology=false] False by default. Set to
trueto opt in to the MongoDB driver's replica set and sharded cluster monitoring engine.
- @param {Boolean} [options.useUnifiedTopology=false] 默认为 False。设置为
true选择加入 MongoDB 驱动程序的副本集和分片集群监控引擎。
There is also an issue on the project GitHub about this error: https://github.com/Automattic/mongoose/issues/8156
项目 GitHub 上也有一个关于这个错误的问题:https: //github.com/Automattic/mongoose/issues/8156
In my case I don't use Mongoose in a replica set or sharded cluster and though the option should be false. But if false it complains the setting should be true. Once is true it still don't work, probably because my database does not run on a replica set or sharded cluster.
就我而言,我不在副本集或分片集群中使用 Mongoose,尽管该选项应该为 false。但如果 false 它抱怨设置应该是 true。一旦为真,它仍然不起作用,可能是因为我的数据库不在副本集或分片集群上运行。
I've downgraded to 5.6.13 and my project is back working fine. So the only option I see for now is to downgrade it and wait for the fix to update for a newer version.
我已经降级到 5.6.13,我的项目又恢复正常了。所以我现在看到的唯一选择是降级它并等待修复程序更新为更新版本。
回答by tamilselvan s
In mongoDB, they deprecated current server and engine monitoring package, so you need to use new server and engine monitoring package. So you just use
在 mongoDB 中,他们弃用了当前的服务器和引擎监控包,因此您需要使用新的服务器和引擎监控包。所以你只需使用
{ useUnifiedTopology:true }
{ useUnifiedTopology:true }
mongoose.connect("paste db link", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
回答by Юрий Светлов
This solved my problem.
这解决了我的问题。
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url, {useUnifiedTopology: true});
回答by Niran Yousuf
You Can Try async await
你可以试试异步等待
const connectDB = async () => {
try {
await mongoose.connect(<database url>, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
console.log("MongoDB Conected")
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
回答by Hassan Ali Shahzad
mongoose.connect('mongodb://localhost:27017/Tododb', { useNewUrlParser: true, useUnifiedTopology: true });
Will remove following errors:-
将删除以下错误:-
(node:7481) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:7481) DeprecationWarning: 当前 URL 字符串解析器已被弃用,并将在未来版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。
(node:7481) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:7481) 弃用警告:当前的服务器发现和监控引擎已弃用,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
回答by Richard Vergis
Add the useUnifiedTopologyoption and set it to true.
添加useUnifiedTopology选项并将其设置为true。
Set other 3 configuration of the mongoose.connectoptions which will deal with other remaining DeprecationWarning.
设置mongoose.connect选项的其他 3 个配置,这些配置将处理其他剩余的DeprecationWarning。
This configuration works for me!
这个配置对我有用!
const url = 'mongodb://localhost:27017/db_name';
mongoose.connect(
url,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
}
)
This will solve 4 DeprecationWarning.
这将解决 4 DeprecationWarning。
- Current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
- Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
- Collection.ensureIndex is deprecated. Use createIndexes instead.
- DeprecationWarning: Mongoose:
findOneAndUpdate()andfindOneAndDelete()without theuseFindAndModifyoption set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#-findandmodify-.
- 当前 URL 字符串解析器已弃用,并将在未来版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。
- 当前服务器发现和监控引擎已弃用,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
- Collection.ensureIndex 已弃用。请改用 createIndexes。
- DeprecationWarning:猫鼬:
findOneAndUpdate()并findOneAndDelete()没有useFindAndModify选项设置为false已被弃用。请参阅:https: //mongoosejs.com/docs/deprecations.html#-findandmodify-。
Hope it helps.
希望能帮助到你。
回答by Steven Ventimiglia
I want to add to this thread that it may also have to do with other dependencies.
我想添加到这个线程,它可能也与其他依赖项有关。
For instance, nothing I updated or set for NodeJS, MongoDB or Mongoose were the issue - however - connect-mongodb-sessionhad been updated and starting slinging the same error. The solution, in this case, was to simply rollback the version of connect-mongodb-sessionfrom version 2.3.0to 2.2.0.
例如,我为 NodeJS、MongoDB 或 Mongoose 更新或设置的任何内容都不是问题 - 然而 -connect-mongodb-session已经更新并开始抛出相同的错误。在这种情况下,解决方案是简单地将版本connect-mongodb-session从版本回滚2.3.0到2.2.0.
回答by Paul Orubebe
i had the same errors popping up each time and this worked for me
我每次都会出现相同的错误,这对我有用
mongoose.connect("mongodb://localhost:27017/${yourDB}", {
useNewUrlParser: true,
useUnifiedTopology: true
}, function (err) {
if (err) {
console.log(err)
} else {
console.log("Database connection successful")
}
});
回答by Alok Srivastav
const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/Edureka',{ useNewUrlParser: true, useUnifiedTopology: true }, (error)=> {
const connectionStatus = !error ? 'Success': 'Error Connecting to database';
console.log(connectionStatus);
});
回答by Miguel Chiau
I was also facing the same issue:
我也面临同样的问题:
I made sure to be connected to mongoDB by running the following on the terminal:
brew services start [email protected]And I got the output:
Successfully started `mongodb-community`
我确保通过在终端上运行以下命令连接到 mongoDB:
brew services start [email protected]我得到了输出:
Successfully started `mongodb-community`
Instructions for installing mongodb at
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/or https://www.youtube.com/watch?v=IGIcrMTtjoU
在
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/或https://www.youtube.com/watch?v=IGIcrMTtjoU 上安装 mongodb 的说明
My configuration was as follows:
mongoose.connect(config.mongo_uri, { useUnifiedTopology: true, useNewUrlParser: true}) .then(() => console.log("Connected to Database")) .catch(err => console.error("An error has occured", err));
我的配置如下:
mongoose.connect(config.mongo_uri, { useUnifiedTopology: true, useNewUrlParser: true}) .then(() => console.log("Connected to Database")) .catch(err => console.error("An error has occured", err));
Which solved my problem!
这解决了我的问题!


