Open In App
Related Articles

How to connect mongodb Server with Node.js ?

Improve Article
Save Article
Like Article

mongodb.connect() method is the method of the MongoDB module of the Node.js which is used to connect the database with our Node.js Application. This is an asynchronous method of the MongoDB module.



Parameters: This method accept two parameters as mentioned above and described below:

  1. Path/URL: The Path of the server of the MongoDB server which is running on a particular port number.
  2. callbackfunction: It returns the err or the instance of the mongodb database for further operation if connection successful.

Installing module:

npm install mongodb --save

Project structure:

Command to run server on particular ip:

mongod --dbpath=data --bind_ip

FileName index.js


// Module calling
const MongoClient = require("mongodb");
// Server path
// Name of the database
const dbname = "conFusion";
MongoClient.connect(url, (err,client)=>{
    if(!err) {
        console.log("successful connection with the server");  
        console.log("Error in the connectivity");

Running command:

node index.js


node index.js
(node:7016) 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 the MongoClient constructor.
(Use `node --trace-deprecation ...` to show where the warning was created)
successful connection with the server
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Oct, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials