What is MongoDB NodeJS Connection: The Guide

on Data Integration, Database Management Systems, MongoDB, Node js • February 16th, 2022 • Write for Hevo

MongoDB Node js

There are several NoSQL databases out there, but MongoDB is the most commonly used, and it is available both as a Cloud Service and for Deployment on Self-Managed Systems. Even though MongoDB isn’t an RDBMS (Relational Database Management System) and doesn’t offer extensive join methods, you can use several other operators to join two collections in MongoDB.

Node.js is a backend, open-source JavaScript runtime environment that can carry out JavaScript code outside a web browser and runs on the V8 Engine.

This blog talks about the steps involved in setting up an NPM MongoDB Connection seamlessly in detail. It also gives a brief introduction to NodeJS and MongoDB before diving into the MongoDB NPM Connection setup steps.

Table of Contents

What is MongoDB?

MongoDB Nodejs: MongoDB Logo
Image Source

MongoDB is a NoSQL database system widely used by companies to develop modern flexible applications. It is an open-source cross-platform document-oriented database because it does not store information in tabular form. Developers use MongoDB Database because of its high flexibility for storing data from modern applications.

MongoDB is developed by MongoDB Inc, and in commercial use since 2009 that help Developers and organizations manage the data flexibly from the applications. It uses JSON-type format documents with optional schemas and helps Developers easily manipulate, access, and manage large datasets of distributed data. 

Key Features of MongoDB

Some of the main features of MongoDB are listed below:

  • Schema-Less: MongoDB is a non-Relational Database that uses documents and collections to store and manage data. Users don’t need a predefined schema.
  • Scalability: MongoDB is easy to scale as per the business requirements because it supports sharding that distributes the large Datasets into smaller chunks using the sharding key.
  • High Performance: MongoDB delivers high performance while querying data as compared to other Databases because of its scalability, indexing, and replication features.

What is Node.js?

MongoDB Nodejs: Nodejs Logo
Image Source

Node.js was developed as an asynchronous event-driven JavaScript runtime that can be leveraged to develop scalable applications. It is primarily used by the developers to develop command-line tools, in addition to server-side scripting for the creation of the dynamic web content before it gets opened on the user’s display. Powered by Google Chrome’s V8 engine, the utility of Node.js exists in the fact that it can process the JavaScript code outside a web browser.

Keeping it in view, Node.js can be considered as a “JavaScript everywhere” regime i.e., integrate Application Development (web) around a solitary Programming Language, as opposed to different programming media being leveraged for the respective stakeholders i.e., Server-side and Client-side codes. Specifically, Node.js is an event-driven architecture focused on enriching and optimizing scalability and throughput within web applications. 

As far as the naming is concerned, .js is referred to as a standard file extension name in JavaScript but Node.js doesn’t refer to a particular file rather it is a generic product name. 

MongoDB Nodejs: Nodejs Use Cases
Image Source

What makes Node.js & NoSQL Database a Great Match?

With the advent of technology, there has been a sharp increase in the number of NoSQL databases like MongoDB and MySQL for storing data. The ability of these databases to store any type of content and specifically in any format is what makes these databases so desirable.

NodeJS and NoSQL databases are a great match because NodeJS can work with both MySQL and MongoDB as databases making it a suitable candidate to be used in tandem with them. If you wish to use these databases with NodeJS, all you have to do is download and use the requisite modules via the NodeJS Package Manager. 

For MySQL, the necessary NodeJS module is “mysql” whereas for MongoDB the required module is called “Mongoose”. With MySQL and MongoDB NodeJS module you can execute the following operations in NodeJS seamlessly:

  • Creation and Closing of Database Connection: With these modules, you can go about creating and closing a database connection quite easily. In either case, you can supply a callback function that can be invoked as and when the “close” and “create” connection methods are carried out.
  • Managing the Connection Pooling: With these modules, you can also specify the number of MySQL database connections that need to be maintained and saved by NodeJS.
  • Data Manipulation: You can carry out various Data Manipulation processes such as deleting, inserting, and updating data.
  • Ease of Execution: Queries can be easily executed to extract data from the respective databases.

Simplify your MongoDB ETL with Hevo’s No-code Data Pipeline

A fully managed No-code Data Pipeline platform like Hevo helps you integrate data from 100+ data sources (including 40+ Free Data Sources) like MongoDB to a destination of your choice in real-time in an effortless manner. Hevo with its minimal learning curve can be set up in just a few minutes allowing the users to load data without having to compromise performance. Its strong integration with umpteenth sources provides users with the flexibility to bring in data of different kinds, in a smooth fashion without having to code a single line. 

GET STARTED WITH HEVO FOR FREE

Check Out Some of the Cool Features of Hevo:

  • Completely Automated: The Hevo platform can be set up in just a few minutes and requires minimal maintenance.
  • Real-Time Data Transfer: Hevo provides real-time data migration, so you can have analysis-ready data always.
  • Connectors: Hevo supports 100+ Integrations to SaaS platforms, files, databases, analytics, and BI tools. It supports various destinations including Amazon Redshift, Firebolt, Snowflake Data Warehouses; Databricks, Amazon S3 Data Lakes, MySQL, SQL Server, TokuDB, DynamoDB, PostgreSQL databases to name a few.  
  • 100% Complete & Accurate Data Transfer: Hevo’s robust infrastructure ensures reliable data transfer with zero data loss.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources, that can help you scale your data infrastructure as required.
  • 24/7 Live Support: The Hevo team is available round the clock to extend exceptional support to you through chat, email, and support calls.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Live Monitoring: Hevo allows you to monitor the data flow so you can check where your data is at a particular point in time.

Simplify your Data Analysis with Hevo today! 

SIGN UP HERE FOR A 14-DAY FREE TRIAL!

How to Connect MongoDB with Node.js?

Here are the steps you can follow to setup MongoDB NodeJS connection seamlessly:

For this example, you can assume the MongoDB data mentioned below:

  • Collection Name: Employee
  • Database Name: EmployeeDB

Here is the snippet for the same:

Documents
{
	{Employeeid : 1, Employee Name : Guru99},
	{Employeeid : 2, Employee Name : Joe},
	{Employeeid : 3, Employee Name : Martin},
}

MongoDB Nodejs Connection Setup: Installing the NPM Module

  • Step 1: For this MongoDB Nodejs Connection step, you need a driver to gain access to Mongo from within a Node application. There are a vast number of Mongo drivers available in the marketplace to choose from. However, MongoDB is the most popular of the lot. To install the MongoDB module, simply execute the following command:
npm install mongodb

MongoDB Nodejs Connection Setup: Configuring the MongoDB Node.js Connection

The code snippet mentioned below talks about how you can open and close the connection to a MongoDB database for MongoDB Nodejs connection:

MongoDB Nodejs: Configuring Connection
Image Source
  • Step 1: The first step is to include the Mongoose module, which can be carried out via the required function. Once this module is in place, you can utilize the necessary functions present in this module to generate connections to the database.
  • Step 2: Next, you need to specify your connection string to the database. Within the Connection String, there are 3 pivotal values to be passed:
    • The first value to be passed is ‘mongodb’. This value specifies that you are trying to establish a connection to a MongoDB database.
    • The next value is ‘localhost’. This value specifies that you are connecting to a database present on the local machine.
    • The final value present is the ‘EmployeeDB’ which is the name of the database defined in your MongoDB database.
  • Step 3: Finally, you need to connect to your MongoDB database. The connection function takes in your URL and can invoke a callback function. This function gets called when the connection is opened to the database. This will allow you to know if the database connection was successful or not.

MongoDB Nodejs Connection Setup: Closing the Connection

  • Step 1: In the function mentioned above, you are writing the string ‘Connection established’ to the console to show that a successful connection has been established.
  • Step 2: Finally, you’ll be closing the connection to your MongoDB database by leveraging the db.close statement. If the code is executed properly, the string “Connected” will be shown in the console as mentioned below for the MongoDB Nodejs connection:
MongoDB Nodejs: Closing Connection
Image Source

How to Start Querying Data from MongoDB using Node.js?

  • Step 1: You can use the MongoDB driver to fetch data from the MongoDB database for establishing a MongoDB Nodejs connection. The following section will depict how you can use the driver to fetch all of the documents from your Employee collection in the EmployeeDB database.

    This is the collection present in your MongoDB database, which contains all the employee-related documents. Every document has an object id, employee id, and Employee name that are used to define the values present in the MongoDB Nodejs document.
MongoDB Nodejs: Query Data Step 1
Image Source
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost/EmployeeDB';

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

    var cursor = db.collection('Employee').find();

    cursor.each(function(err, doc) {

        console.log(doc);

    });
});
  • Step 2: First, you are creating a cursor in the previous MongoDB Nodejs connection step. A cursor is a pointer that can be used to point to the numerous records fetched from a database. The cursor can then be used to iterate through the different records in a database. Here, you will be defining a variable name called cursor which would then be used to store the pointer to the records extracted from the database.

    You also have the facility to specify the collection ‘Employee’ from which you wish to extract the records. The find() function in MongoDB Nodejs can be used to specify that you wish to retrieve all of the documents from the MongoDB collection for the MongoDB Nodejs connection.
  • Step 3: Next, you will be iterating through your cursor and for each document in the cursor, you are going to execute a function. The function is simply going to print the contents of each document to the console. Here’s the output of the aforementioned command:
MongoDB Nodejs: Query Data Step 3
Image Source
  • Step 4: From the output of this MongoDB Nodejs connection step, it is evident that all the documents from the collection have been retrieved. This is made possible by leveraging the find() method of the MongoDB connection (db) and iterating through all of the documents using the cursor.

How to perform MongoDB CRUD Operations using Node.js?

In this section, you’ll see how MongoDB NodeJS Integration let you quickly implement pivotal application functionality like CRUD (Create, Read, Update, Delete) operations.

  • Step 1: First, you need to go to your desired folder where you’ll be creating a new project for the MongoDB Nodejs CRUD operations. Type in npm init and for the project name, you can use node-mongo-intro. You can accept all the other defaults and move on to the next step. Next, you need to add all the dependencies you need.

    In the project directory, you just generated, type npm install mongodb polka –save. This will save both the Polka HTTP Server to be used for handling HTTP requests and the MongoDB NodeJS driver to allow your project to access MongoDB.
  • Step 2: Next, you need to edit the package.json file to include a script, as mentioned below in Listing 1:
"scripts": {
	"test": "echo "Error: no test specified" && exit 1",
	"start": "node src/index" // <-- add this line
  },
  • Step 3: Next, create a /node-mongo-intro/src/index.js file for MongoDB Nodejs CRUD operation, and place the contents of Listing 2 in it. Here’s what Listing 2 looks like:
const polka = require('polka');

polka()
  .get('/create', (req, res) => {
	res.end(`works`);
  })
  .listen(3000, err => {
	if (err) throw err;
	console.log(`> Running on localhost:3000`);
  });
  • Step 4: Start the server with npm start. The server will listen on port 3000. You can verify it with curl http://localhost:3000/create. If you do, you will be able to view the response of “works”.

How to Create/Insert a New Document in MongoDB Collection?

  • Step 1: Now, you’re going to execute a simple insert. Update the index.js file to look like Listing 3 as mentioned below for this MongoDB Nodejs CRUD operation:
const polka = require('polka');
const { MongoClient } = require("mongodb");

polka()
  .get('/create', (req, res) => {
	const client = new MongoClient("mongodb://localhost:27017");
	async function run() {
  	try {
    	await client.connect();
    	const database = client.db("intro");
    	const collection = database.collection("quotes");

    	const result = await collection.insertOne({"quote":"Life is what happens to you while you're busy making other plans."});
    	res.end(JSON.stringify(result));
  	} catch (e) {
    	console.log("Error: " + e);
  	} finally {
    	await client.close();
  	}
	}
	run().catch(console.dir);
  })
  .listen(3000, err => {
	if (err) throw err;
	console.log(`> Running on localhost:3000`);
  });
  • Step 2: This MongoDB Nodejs CRUD operation code opens up a connection to the MongoDB instance on your local system, then specifies a collection (“quotes”) and a database (“intro”). A collection is similar to a table in a relational database.

    Next, the code inserts a document and sends the results back in an HTTP response.
  • Step 3: Now, you need to stop and restart the node server by hitting Ctrl-C. Follow this up by running this command at the command line:
npm run startcurl http://localhost:3000/create
  • Step 4: Now to verify the insert, open the mongo shell with mongo, and enter the command use intro. This switches to the intro database that was automatically generated.

    Now enter the db.quotes.find() command, and you’ll see that the record has been inserted. You can notice that MongoDB automatically generates a unique ID on the “_id” field. 

How to Update a MongoDB Document?

  • Step 1: Here’s the code snippet you can execute for updating a document using MongoDB Nodejs:
.get('/update', (req, res) => {
	const client = new MongoClient("mongodb://localhost:27017");
	async function run() {
  	try {
    	await client.connect();
    	const database = client.db("intro");
    	const collection = database.collection("quotes");

    	const updateDoc = {
      	$set: {
        	author:
          	"John Lennon",
      	},
    	};

    	const result = await collection.updateOne({}, updateDoc, {}); // <-- empty filter matches all docs
    	res.end("Updated: " + result.modifiedCount);
  	} catch (e) {
    	errCallback(e);
  	} finally {
    	await client.close();
  	}
    }
	run().catch(console.dir);
  })
  • Step 2: The code snippet mentioned above connects to the database, then creates an updated document. This document lets MongoDB Nodejs know what needs to be changed, by mentioning a $set field consisting of an object with the values and fields to change.

    For this case, you can set the author field to “John Lennon”, the quoter of the quote in question. Next, the snippet uses updateOne() function to execute the update document. That final empty-object argument can be identified as the filter. Here, you want to match all your documents, so you can leave them blank.

How to Retrieve a MongoDB Document?

  • Step 1: To execute the retrieve operation for a MongoDB document, add the .get() mapping as shown in Listing 4.
.get('/retrieve', (req, res) => {
	const client = new MongoClient("mongodb://localhost:27017");
	async function run() {

  	try {
    	await client.connect();
    	const database = client.db("intro");
    	const collection = database.collection("quotes");

    	const cursor = collection.find({}, {});

    	let items = [];
    	await cursor.forEach(function(doc){
      	items.push(doc);
    	});
    	res.end(JSON.stringify(items));
  	} catch (error){
    	console.warn("ERROR: " + error);
	    if (errCallback) errCallback(error);
  	} finally {
    	await client.close();
  	}
	}
	run().catch(console.dir);
  })
  • Step 2: Listing 4 gets connected in the same fashion as Listing 3, then sends out a find command along with an empty query. This means that it matches all the documents.

    Next, it takes the response and converts it into an array to be sent back to the client.
  • Step 3: You can test the new endpoint, after starting and stopping the server again, with curl http://localhost:3000/retrieve and you can view that the collection has been returned. 

How to Delete a MongoDB Document?

  • Step 1: This is what the mapping for deletion operation looks like using MongoDB Nodejs:
.get('/delete', (req, res) => {
	const client = new MongoClient("mongodb://localhost:27017");
	async function run() {
  	try {
    	await client.connect();
    	const database = client.db("intro");
    	const collection = database.collection("quotes");
    	const query = { };
    	const result = await collection.deleteOne(query);
    	if (result.deletedCount === 1) {
      	res.end("Successfully deleted one document.");
    	} else {
      	res.end("Deleted 0 documents.");
    	}
  	} finally {
    	await client.close();
  	}
	}
  • Step 2: Here, you have used an empty query to match all the documents within the “quotes” collection. The async collection.deleteOne() function gives a result telling you how many documents have been affected.
  • Step 3: To verify this, restart the server and issue a new curl command:
curl http://localhost:3000/delete
  • Step 4: You can check that the document has been deleted with curl http://localhost:3000/retrieve. This concludes the discussion on the CRUD operations.

Conclusion

This blog talks in detail about the steps involved in setting up a connection between MongoDB and NodeJS in a seamless fashion. It also gives a brief introduction to MongoDB and NodeJS along with the CRUD operations that can be carried out. It also talks about why using Nodejs for NoSQL databases is beneficial. This includes understanding and verifying the creation, retrieval, updating, and deletion operations using MongoDB Nodejs.

Extracting complex data from a diverse set of data sources to carry out an insightful analysis can be challenging, and this is where Hevo saves the day! Hevo offers a faster way to move data from Databases or SaaS applications like MongoDB into your Data Warehouse or a tool to be visualized in a BI tool. Hevo is fully automated and hence does not require you to code.

VISIT OUR WEBSITE TO EXPLORE HEVO

Want to take Hevo for a spin?SIGN UP and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

No-code Data Pipeline for MongoDB