NodeJS MongoDB Connection

Nodejs need relevant modules / packages of mongodb to connect and access the database.

Install below npm package, so that we can import it in our application.
npm install mongodb --save
Create a file “node-mongo.js” with below code.

It will make a connection with Mongo Database and create a schema /database.

var mdb = require('mongodb').MongoClient;  
var con = "mongodb://localhost:27017/employee";  
mdb.connect(con,{ useNewUrlParser: true, useUnifiedTopology: true }, function(err, db) {  
if (err) throw err;  
console.log("Database created!");  
Below SS shows the output for this program.


Below SS shows the output from MongoDB.


Subscribe Now