Home Blog Connecting MongoDB with NodeJs
Connecting MongoDB with NodeJs

Connecting MongoDB with NodeJs

Javascript language is very popular and is amongst the most used coding languages around the world. Having 80+ frameworks used across different platforms and for different purposes. Node JS is one of the most popular and powerful framework of Javascript.

It was developed in 2009 by Ryan Dahl. It is an open source and cross-platform framework that provides a server-side Javascript runtime environment. It is widely used to create web applications. 

Now since we are talking of the applications, one of the major parts of any application is its data. To store this data you need a database. Since the past few years there has been a major development in the database sector.

The concept of NoSQL(Non-tabular databases) is getting widely accepted and used. One of the most used and famous NoSQL is MongoDB. It is used to handle a large set of data be it of any format. In this blog, we will see how we can connect the MongoDB database with NodeJS.

Installation

Before starting with the connection you need to make sure that you have both the NodeJS and MongoDB installed and setup properly. 


Setting Up NodeJS project:

First, You must ensure that you have NodeJS installed in your system. To install you need to open the cmd terminal and if you are using Linux or any associated OS then you can run the command:

=> sudo apt install nodejs 


For Windows you can simply visit https://nodejs.org/en/download and download the version you find most compatible with your system and then follow the process.

Now that you have NodeJS installed in your system, create a directory/folder in which you wish to set up your app. Now navigate to the folder/directory and open your cmd terminal.

Initialize a new Node.js project by running “npm init ” and following the prompts. This will create a `package.json` file that keeps track of project dependencies and other configuration details.

 Subscribe our newsletter to learn more about MongoDB.


Setting Up MongoDB:

To begin, you need to install MongoDB on your machine. Visit the official MongoDB website and download the appropriate version for your operating system. Follow the installation instructions provided.

Once the MongoDB is installed in your system now you need to set up the connectors/drivers to connect it with the NodeJS. Now, open the cmd terminal in your project directory and run the command:

“npm install MongoDB”

This will download and install the MongoDB driver as a dependency for your project.


Now that you have installed everything in your project you need to step inside your project and create a file in which you can write down your connection code. Let’s just say you created a file named Demo.js. Let’s just step into Demo.js and write some code:


=>  var mongo = require('mongodb');


this will let you import the mongodb into your file


Connecting the database

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:4000/my_db";

 

MongoClient.connect(url, function(error, db) {

  if (error){

throw error;

}

  console.log(" Database connected! !");

            db.close();

});


MongoClient here is a class of mongodb modules which help in setting connections with the mongodb database. In the url we give the urls of the server following with the database name, which in this case will be my_db. 

This code lets you connect with the database my_db. It creates the database if it is not present and then connects to it. On successful connection you will receive a console message of “Database Connected” else you will get an error.

After the connection, it becomes necessary to close the database, for which we have used db.close() which is an inbuilt function.Now that we have a connection we can perform all the CRUD operations with the database using NodeJS. 


So, in this article we came across how we can set up a connection between NodeJS and MongoDB. MongoDB's flexibility and Node.js's event-driven and non-blocking nature makes them a powerful combination for building scalable and efficient applications.

Share your thoughts, queries, or suggestions in the comments below or reach out to us at Kanak Infosystems
Happy coding!



Get in touch with us!

Protected by reCAPTCHA, Privacy Policy & Terms of Service apply.
Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.