Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 483
Momin Avatar asked Aug 18 '19 16:08

Momin


People also ask

How does node connect to MongoDB?

To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database if it does not exist, and make a connection to it.

Can we use MongoDB with node js?

The MongoDB Node. js Driver allows you to easily interact with MongoDB databases from within Node. js applications. You'll need the driver in order to connect to your database and execute the queries described in this Quick Start series.

Which method is used to connect from Nodejs to MongoDB?

No-SQL databases allow developers to send and retrieve data as JSON documents, instead of SQL objects. To work with MongoDB in a Node. js app, we can use Mongoose.

Which drivers are useful to connect node js with MongoDB?

Connect your Node. js applications to MongoDB and work with your data using the Node. js driver. The driver features an asynchronous API that you can use to access method return values through Promises or specify callbacks to access them when communicating with MongoDB.


4 Answers

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

like image 74
Bivin Vinod Avatar answered Oct 02 '22 10:10

Bivin Vinod


My advice is to leave it as it is (maybe place a warning). The useUnifiedTopology: true option does not work correctly.

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.

Check this

like image 39
Сергей Дудко Avatar answered Oct 02 '22 11:10

Сергей Дудко


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.

like image 20
Devashish Avatar answered Oct 02 '22 09:10

Devashish


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-session had been updated and starting slinging the same error. The solution, in this case, was to simply rollback the version of connect-mongodb-session from version 2.3.0 to 2.2.0.

UPDATE: The issue is now fixed in [email protected].

enter image description here

like image 44
Steven Ventimiglia Avatar answered Oct 02 '22 11:10

Steven Ventimiglia