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

查看:18
本文介绍了使用节点服务器连接到 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天全站免登陆