通过节点服务器连接到MongoDB的警告 [英] Warning on Connecting to MongoDB with a Node server

查看:127
本文介绍了通过节点服务器连接到MongoDB的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,以通过本机驱动程序连接mongodb,该本机驱动程序已安装 npm install mongodb --save

I wrote following code to connect mongodb through native driver which has been install with 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}`);
  }
);

当我在终端上写 node server.js 我收到以下错误

When I write on the terminal node server.js I got following error


(node:3500)DeprecationWarning:当前的服务器发现和监视引擎已弃用,并将在未来版本。要使用新的服务器发现和监视引擎,请将选项{useUnifiedTopology:true}传递给MongoClient.connect。
已连接MongoDB:mongodb://127.0.0.1:27017
数据库:权力游戏

(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

数据库已连接,但是如何摆脱警告

The database is connected, but how can I get rid out from the warning

推荐答案

检查您的mongo版本

Check your mongo version

 mongo --version

如果使用的版本> = 3.1.0,请将mongo连接文件更改为->

If you are using version >= 3.1.0 change you mongo connection file to ->

 MongoClient.connect("mongodb://localhost:27017/YourDB", {
   useNewUrlParser: true,
   useUnifiedTopology: true
 })

有关3.2.1中添加的 useUnifiedTopology 选项的详细信息,请参见 https://github.com/mongodb/node-mongodb-native/releases/tag/v3.2.1

For details about the useUnifiedTopology option added in 3.2.1, see https://github.com/mongodb/node-mongodb-native/releases/tag/v3.2.1

这篇关于通过节点服务器连接到MongoDB的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆