Nodejs Connection in MongoDB

To create connection in Nodejs with MongoDB is easy like PHP MySQL Connection. Nodejs Connection in MongoDB.

MongoDB will create the database if it does not exist, and make a connection to it.

db.connection.js

var MongoClient = require('mongodb').MongoClient;
//Create a database named "mydb":
var url = "mongodb://localhost:27017/DBNAME";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

Now check the connection using cmd command . So write below cmd for testing the connection

C:\Users\Your Name>node db.connection.js

If everything will be right then you will get the below result

Database created!

Read our More Tutorials on Nodejs and ReactJS

Routing in Node.js

Live Search with React and NodeJS

CRUD Operation Using React & Nodejs

Building a Simple CRUD App with NodeJS

Leave a Reply

Your email address will not be published. Required fields are marked *