node.js 使用节点服务器连接到 MongoDB 的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57546635/
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
Warning on Connecting to MongoDB with a Node server
提问by Momin
Connecting with MongoDB native driver
连接MongoDB原生驱动
I wrote following code to connect mongodb through native driver which has been install with npm install mongodb --save
我编写了以下代码来通过已安装的本机驱动程序连接 mongodb npm install mongodb --save
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://127.0.0.1:27017";
const dbName = "game-of-thrones";
let db;
MongoClient.connect(
url,
{ useNewUrlParser: true },
(err, client) => {
if (err) return console.log(err);
db = client.db(dbName);
console.log(`Connected MongoDB: ${url}`);
console.log(`Database: ${dbName}`);
}
);
When I write on the terminal node server.jsI got following error
当我在终端上写时,node server.js出现以下错误
(node:3500) 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 MongoClient.connect. Connected MongoDB: mongodb://127.0.0.1:27017 Database: game-of-thrones
(node:3500) DeprecationWarning:当前的服务器发现和监控引擎已被弃用,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient.connect。连接的 MongoDB:mongodb://127.0.0.1:27017 数据库:权力的游戏
The database is connected, but how can I get rid out from the warning
数据库已连接,但如何摆脱警告
回答by Bivin Vinod
Check your mongo version
检查您的 mongo 版本
mongo --version
If you are using version >= 3.1.0 change you mongo connection file to ->
如果您使用的版本 >= 3.1.0,请将您的 mongo 连接文件更改为 ->
MongoClient.connect("mongodb://localhost:27017/YourDB", {
useNewUrlParser: true,
useUnifiedTopology: true
})
For details about the useUnifiedTopologyoption added in 3.2.1, see https://github.com/mongodb/node-mongodb-native/releases/tag/v3.2.1
useUnifiedTopology3.2.1 中新增的选项详见https://github.com/mongodb/node-mongodb-native/releases/tag/v3.2.1
回答by Сергей Дудко
My advice is to leave it as it is (maybe place a warning). The useUnifiedTopology: trueoption does not work correctly.
我的建议是保持原样(也许会发出警告)。该useUnifiedTopology: true选项无法正常工作。
More precisely, in the event of a loss of connection to the DBMS, it will never be restored. Current version 3.3.3 does not solve this problem.
更准确地说,在与 DBMS 的连接丢失的情况下,它将永远不会恢复。当前版本 3.3.3 没有解决这个问题。
回答by DEVas
I got the same error and resolved using the below template.
我遇到了同样的错误并使用以下模板解决了。
var MongoClient = require('mongodb').MongoClient
const client = new MongoClient(uri, {useUnifiedTopology: true});
client.connect().then((client)=>{
var db = client.db('db_name')
db.collection('collection_name').find().toArray(function (err, result) {
if (err) throw err
console.log(result);
})
})
This worked for me. and now it's not showing any DepricationWarning.
这对我有用。现在它没有显示任何 DepricationWarning。
回答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.
UPDATE: The issue is now fixed in [email protected].
更新:该问题现已在[email protected].
回答by Subhash Chaudhary
Replace yourdbnamewith your variable or just link of your mongodb..
替换yourdbname为您的变量或只是您的mongodb..
mongoose.connect(yourdbname, {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true })
.then(console.log("mongodb connected successfully...."))
.catch(err =>console.log(err));
回答by pdwonline
Tried all of these. Code below shows 'Finished' imediately. No error but no result either. After a while I get:
这些都试过了。下面的代码立即显示“完成”。没有错误,但也没有结果。一段时间后,我得到:
Promise { <pending> }
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [credentials] is not supported
Finished
(node:111766) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
code (where username and password are replaced by the actual data):
代码(其中用户名和密码被实际数据替换):
const url = 'mongodb://username:password@localhost:27017';
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url, {useUnifiedTopology: true});
client.connect().then((client)=>{
var db = client.db('MYDB');
console.log ('Retrieving data');
db.collection('products').find().toArray(function (err, result) {
if (err) throw err
console.log(result);
});
});
console.log('Finished');
my package.json contains:
我的 package.json 包含:
"connect-mongo": "^3.2.0",
"mongodb": "^3.4.1"
回答by Momin
A little bit update can remove the error while connecting with MongoDBnative driver
一点点更新可以消除连接MongoDB原生驱动时的错误
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://127.0.0.1:27017";
const dbName = "game-of-thrones";
let db;
MongoClient.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
if (err) return console.log(err);
db = client.db(dbName);
console.log(`Connected MongoDB: ${url}`);
console.log(`Database: ${dbName}`);
}
);


