Home » Build a Project using MongoDb and Node Js

Build a Project using MongoDb and Node Js

MongoDb and Node Js

This post will cover two very popular technologies – MongoDB and Node.js. MongoDB is a document-oriented database and Node.js is a runtime environment for running JavaScript on the server.

The main goal of this introduction is to demonstrate how easy it can be to get started with these technologies and how powerful they are when combined together.

We will cover the following topics:

1- What is MongoDB?

2- Why use MongoDB?

3- How does it compare to other relational databases such as MySQL, PostgreSQL or Microsoft SQL Server?

4- How does it compare to NoSQL databases such as Elasticsearch, Cassandra or Redis?

5- What do we need in order to run a simple app using Node.js and MongoDB together?

What is MongoDB?

MongoDB is a free and open-source cross-platform document-oriented database developed by MongoDB Inc.

MongoDB is an open source, document oriented database designed to help developers and companies store and manage data in a better way. It is developed and maintained by MongoDB Inc. which was founded in 2007 by Stuart J. Johnston, Dev Ittycheria, Dwight Merriman, Eliot Horowitz, Matt Tolman, Kevin Ryan etc. The company has its headquarters at Palo Alto in California.

MongoDB is designed for speed with features like dynamic schema that can be changed without downtime or data loss with few clicks of the mouse on the GUI interface called Ops Manager which also simplifies operations on clusters of databases). It stores data in JSON-like documents, meaning fields can vary from document to document and records are never removed automatically. MongoDB uses indexes, which are like pointers or lists that point to all the documents that match certain criteria, to speed up queries.

Why use MongoDB?

MongoDB is a popular NoSQL database, which is one of the most common databases for storing JSON data.

MongoDB stores data in JSON documents, which are analogous to rows in relational databases. The documents are stored in collections which are analogous to tables. MongoDB isn’t a SQL database but it does provide an API that lets you perform queries on the data with SQL-like syntax.

MongoDB is popular because it can scale easily and its schemaless design makes it easy to store any type of data without the need to create tables or columns beforehand. It also offers high availability with no single point of failure, automatic recovery from failures, and no planned downtime for upgrades or maintenance work.

MongoDB has a number of features that make it more accessible to users not familiar with the relational databases. These include the ability to store objects in binary format, store documents in JSON format, or have one-to-one relationships with documents in collections.

How does it compare to other relational databases such as MySQL, PostgreSQL or Microsoft SQL Server?

PostgreSQL is a powerful and popular relational database that is used by organizations and individuals for various purposes.

MySQL is a popular open-source relational database that was originally developed by MySQL AB. It has now been acquired by Oracle Corporation and was formerly known as Oracle MySQL.

Microsoft SQL Server is a proprietary database management system developed by Microsoft to support their own needs of storing data in different systems such as Windows Server, Azure, and Office 365.

How does it compare to NoSQL databases such as Elasticsearch, Cassandra or Redis?

NoSQL databases are different from relational databases. NoSQL databases are designed to manage the large amount of data that is not structured. It provides an alternative to traditional database management systems.

Comparing to the popular NoSQL databases, CouchDB is considered as a lightweight database because it doesn’t consume much system resources and easy to use with Javascript programming language. MongoDB is an open source database, which is widely used in production environments for managing data across multiple machines and geographies.

What do we need in order to run a simple app using Node.js and MongoDB together?

We need a web browser, which can be installed on any device with internet connection, and has support for JavaScript. We also need an IDE (Integrated Development Environment) for writing code. And finally, we should have some basic knowledge about JavaScript programming language.

First, we need to install Node.js and MongoDB on our computer or on a server. We can use the Node package manager to install these two programs.

Second, we need to create a Node.js app and add some code to it. The following code will be used for this example:

let express = require('express');
let mongo = require("mongoose");
let app = express();

// Connecting with MongoDB

mongoose .connect("mongodb://localhost:27017/test") .once("connected", function() {});

How to insert a Document with Mongodb?

The MongoDB driver for Node.js can be installed using npm:

npm install mongodb

A connection to a running MongoDB server can be created using the following command:

var db = new Db('name', 'localhost', 27017, {wiredTiger: true});

The following code inserts a Document into the collection named “test”:


Just like SQL, you can insert new documents into MongoDB through the use of the insert command. For example, let’s say we want to insert a document called “my_document” with an array of three fields: “name, age and height”. We would type into the shell on our terminal:

INSERT INTO my_document (name, age, height) VALUES ('John', 25, 1.7)

Notice that on the fourth line we type in INSERT INTO my_document (name, age, height). This is all that you need to do to make sure that your document gets inserted into MongoDB

How to Retrieve all Documents with Mongodb?

There are many reasons for you to learn how to retrieve all documents with MongoDB. One of the most common reason is, you want to know what the data look like in your database.

The following are some steps on how to retrieve all documents with MongoDB:

1) Create a collection called “Documents”.

2) Insert a document into it.

3) Run an aggregation query which shows the count of all documents in that collection.

4) Run a findOne() query on that particular document in order to get its fields and values.

{ name:"Acil" },
{_id:0, mfdcountry:0,cno:0 }

How to Find Documents with a Query Filter ​with Mongodb?

As we know, Mongodb is a NoSQL database and MongoDB query language is very powerful. It indeed supports the HAVING clause, but does not support the WHERE clause.

In order to find documents with a query filter, we need to use the $where function in the $project operator.

db.inventory.find( {} )

How to Updating a Document ​with Mongodb?

The step by step process on how to update a document with Mongodb:

1) Create two collections in MongoDB.

2) Create the document {“name” : “John Smith”, “age” : 25}

3) Create the document {“name” : “Mary Jones”, “age” : 29}

4) Add both documents into the first collection

5) Update the second document’s age to 30 using $inc operator.

{"EMPID" :20},
{$set:{"Employee_Name" :"Acil"}});
WriteResult({"nMatched": 1,  "nUpserted: 0, "nModified":1 })

How to Removing a Document ​with Mongodb?

To remove a document, the following steps are needed:

1. Log in to MongoDB.

2. Find the document you wish to remove by using find().

3. Delete the document by using deleteOne() command.


Related Blogs