Role of MongoDB In MERN/MEAN Stack
This blog is about how to initialize the Package.json file, use MongoDB, and create a database. Plus, how the MERN stack works in 3-tier architecture.
Join the DZone community and get the full member experience.
Join For FreeMEAN/MERN stack(s) is/are becoming quite popular these days. But did you ever wonder about the reason behind this sudden surge in their popularity? Well, let me enlighten you.
MERN/MEAN is a combination of four languages. If a developer learns the MERN/MEAN stack, it means the developer is covering four aspects of programming - MongoDB, Express.js, Reactive/Angular, and finally the Node.js.
All the languages have their own distinctive set of features that make them so special and in-demand but what does MongoDB do in the MERN/MEAN stack? In this blog post, you will learn about the role of MongoDB in the MERN/MEAN stack and how it works. So, let’s get right into it.
How to Initialize a Package.json File?
First of all, install the npm in your system. Then, you have to create a new project file and go to the project folder in the command terminal. Type the following command to initialize a package.json file. The package.json file holds the important metadata and dependencies of your entire project so we must initialize it.
npm init
name: (nodejs-express-mongodb)
version: (1.0.0)
description: Node.js Restful CRUD API with Node.js, Express and MongoDB
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, mongodb, rest, api
author: Invo
license: (ISC)
Is this ok? (yes) yes
A typical package.json file looks like this:
You can also install modules from npm, and they will show in the package.json file. After making the project file, let’s move to MongoDB.
MongoDB: Cross-platform, Document-oriented Database
MongoDB is a NoSQL database in which each record consists of key-value pairs. These key-value pairs are similar to JSON (JavaScript Object Notation). It's a flexible platform that allows the users to create tables, schema, databases, etc. Documents that can be uniquely identified by a primary key make a basic unit for MongoDB.
When you install MongoDB in the system, you can also use Mongo Shell. Mongo Shell provides the user interface for Javascript. Users can easily carry out the operations such as querying, updating records, and deleting records, from the Mongo shell.
Why Should You Use MongoDB for Your Project?
There are many reasons to go for MongoDB for your project and some of them include:
- MongoDB is a fast and document-oriented database that can easily index documents. It gives a faster response than other databases.
- Data is scalable in MongoDB. The large data can be divided into small patches to easily manipulate.
- MongoDB uses Javascript which is one of its core advantages as JavaScript is the most popular programming language in the world.
- In this database, any kind of data is in a separate document which makes it easy to manage/categorize the whole data.
- MongoDB is very easy to set up.
- It supports document models that make it faster and reliable.
- The most important aspect is the data stored in the JSON form. A JSON file has the following characteristics:
- Objects, Object members, arrays, values, and strings.
- JSON syntax is easy to use.
- It has a wide range of browser compatibility.
- You can easily share data (images, videos, documents) of any size.
How to Create Database in MongoDB
First of all, you can easily create a database by using the following “use” command:
use database_name;
If there is no collection table exists in the database, you can easily create a table by the following command:
db.createCollection("collection_name");
When it comes to entering the records in the database, you can use the “insert” command.
db.collection_name.insert
(
{
"id" : 1,
"Name" : "Micheal",
"Department": "Technical",
"Organization": "Invo"
}
);
Lastly, if you want to query a document, you can write the following command:
db.collection_name.find({Name : "Klaus"}).forEach(printjson);
How Does the MERN/MEAN Stack Work in 3-tier Architecture?
The MERN/MEAN stack creates a 3-tier architecture - frontend, backend, and database:
React.js (Front-end Web Framework)
React.js is the top tier of the MERN/MEAN stack. It is one of the popular Javascript frameworks that are responsible for creating dynamic client-side applications in HTML. It allows you to build complex interfaces through simple components and renders those components as HTML in the backend server.
React handles data-driven interfaces that use minimal code. It has all the bells and whistles that a web framework should have --- error handling, great support for forms, events, lists, and many more.
Express.js and Node.js Server Tier
The next tier consists of Express.js and Node.js. Express.js runs inside the Node.js framework. It acts as a “fast, unopinionated, minimalist web framework for Node.js,” and that is indeed exactly what it is. Express.js has tools to handle URL routing, HTTP requests, and responses.
If you make XML HTTP Requests (XHRs) from your React.js front-end, you can easily connect to the Express.js function that empowers your application. These functions use Node.js drivers of MongoDB via callback functions to access and update data in the MongoDB database.
MongoDB Database Tier
If your application stores data in any form --- user profiles, content, comments, images, uploads, events, then you must need a database that can easily work with Express, Node, and React. Now, this is why MongoDB is important in the MERN/MEAN stack.
A JSON document is created in your React.js front-end. It can be sent from the front end to the Express.js server. When the document is reached in the backend server, it can be processed and stored directly in MongoDB for later retrieval purposes.
Conclusion
In the web development process, MERN/MEAN stack has become increasingly popular as they are super compatible with each other. MongoDB works really fine with other technologies such as React, Node, and Express and that’s why it is in the MERN stack. All the data generated from the application is stored in the database. The user can access the data and update it by using an interface.
Opinions expressed by DZone contributors are their own.
Comments