What is MongoDB and How does it Work?

In this blog, we will learn about the most popular NoSQL database MongoDB.

What is NoSQL Database?

NoSQL databases are non-relational and can be used for unstructured data. Unlike SQL Database which stores the data in rows and column format, NoSQL Database uses document-oriented, key-value, graph-based formats to store the data.

What is MongoDB?

MongoDB is an open-source database and by far the most popular NoSQL Database. It stores the data in key-value pair format and uses BSON as a document storage format, BSON is the binary style of JSON Documents.

Here is an example of a document stored in MongoDB that is considered as a row in SQL Database:

If you need to store this kind of data in an SQL database you will need to create two tables

1. User

2. Address

You will need to add a relation between these two tables using the foreign key. But in MongoDB, it can be stored in one Document only.

Database Structure of MongoDB:

Document: A Set of key-value pairs can be called a Document. Documents are associated with dynamic schema and the benefit associated with dynamic schema is that all the documents in a single collection do not have to possess the same structure.

Collection: A group of documents can be called a collection. Collections are equivalent to tables in SQL Database. There are no schemas when it comes to the collection in NoSQL Database. But the documents within the collection are meant for serving the same goal.

How MongoDB Works:

MongoDB works mainly on two layers:

1. Application Layer

2. Data Layer

Application Layer: The application layer is also known as the Final Abstraction Layer. This Layer consists of two parts, the first is Frontend and another one is Backend. The Frontend is the place where the users with the help of mobile or web interact with MongoDB. The Backend contains a server that is used to conduct server-side logic and also contains a driver or mongo shell to interact with the MongoDB server using the queries.

Data Layer: The queries sent from the backend server are received by the MongoDB server in the Data Layer. MongoDB server further passes these queries to Storage Engine. Storage Engine manages how data is stored in the memory and on the disk. Storage Engine is also responsible to read or write data in the memory.