Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

node.js - Warning on Connecting to MongoDB with a Node server

Connecting with MongoDB native driver

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}`);
  }
);

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

(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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check your mongo version

 mongo --version

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...