Introduction to MongoDB | Part 1

vaishnavi pandey
Last Updated: May 13, 2022

Introduction

MongoDB is document-oriented, no sequel(No SQL) database. It was released in August 2009. MongoDB replaces the concept of rows of conventional relational data models with something known as documents.

 

MongoDB offers developers the flexibility to work with evolving data models. Since it is document-based, it allows embedded documents, arrays and represents complex hierarchical relationships using a single record.

 

It is also schema-free, which means that the keys defined in the document are not fixed. As a result, massive data migration can be ruled out. 

 

You must be wondering, when we already have SQL, why does NoSQL or MongoDB come into the picture?

 

Keep reading, and we'll explain everything in this article.

 

What is MongoDB?

MongoDB is an extensively used NoSQL database. It stores data in a document structure where each document looks like a JSON object. 

 

Figure 1 shows a simple representation of the JSON object.

 

What Is MongoDB? | MongoDB

Figure 1

 

Why and When do we use MongoDB?

MongoDB is used when there are one-many relationships between entities. For instance, the same user can have many posts on Instagram. In this case, all the data of the user will be stored as an object as shown below:

 

{       

     username: user123,

     first_name: "Jane",

     last_name: "Doe",

     posts: [

     {  

         img: "https://www.examapleofimage.com/images/example_logo1.jpg",

         description: "Random picture of Jane",

         created:1635153252

     },

     {  

         img: "https://www.examapleofimage.com/images/example_logo2.jpg",

         description: "Another Random picture",

         created:1635153253

     },

]

}

 

In the above example, the posts are stored in the form of an array of objects. Since this example contains one "user" to many "posts" relationships, it is easier to map out MongoDB.

 

Features of MongoDB

Some of the promising features of MongoDB are given below:

Document Oriented Structure

It is a non-relational database that stores data in a JSON-like structure called BSON.

 

Flexibility  

MongoDB stores data flexibly. That is, each document is allowed to have different fields for a given collection of documents. Moreover, the data structure can be changed whenever necessary. 

 

In the example given below, the first product does not have the field “reviews” as there were no reviews given by the customers for it yet. However, the second product does have a field called “reviews” with an array of objects as its value.

 

{

product_id:  1,

product_description: "125 mL Anti Dandruff Shampoo bottle",

product_price: 60

}

 

{

product_id:  2,

product_description: "50 mL Jar of honey",

product_price: 40,

reviews: [

{

customer_id: jane222,

comment: "It is tasty.",

rating: 4,

created: 16322152133

},

{

customer_id: john222,

comment: "It is okayish.",

rating: 3,

created: 16322152136

 

},

]

}

 

If a customer gives a review for the first product later, then a review field can be created to store the review as shown below:

 

{

product_id:  1,

product_description: "125 mL Anti Dandruff Shampoo bottle",

product_price: 60,

reviews: [

{

customer_id: doe222,

comment: "It is awesome.",

rating: 4.5,

created: 16322152133

}

]

}

 

Free And Open Source

You can download MongoDB Server on your local system for free. You can even deploy it in the cloud using MongoDB Atlas. You can use the free tier of MongoDB Atlas in your mini projects for testing, which provides 500MB space.

 

Horizontal Scalability

Since MongoDB is a NoSQL Database, it is horizontally scalable. Unlike Vertical Scaling,  where a single server with large capacity is used, Horizontal Scaling uses many servers and data is distributed across these servers.

 

Ad hoc queries, indexing, and real-time aggregation 

These allow access and analysis of data easily and quickly.

 

Working of MongoDB

MongoDB work in two layers

Application Layer

Application Layer has two parts:

  • Frontend (User Interface) where the user uses MongoDB with the help of a Web or Mobile.
  • Backend (server)  contains a server and mongo shell to interact with the MongoDB server with the help of queries.

 

Data layer

These queries are sent to the MongoDB server present in the Data Layer. Now, the server receives the queries and passes them to the storage engine. The storage engine is responsible for reading or writing the data in the files or memory.

Applications Of MongoDB

Some of the essential applications of MongoDB are given below:

  1. Content Management Systems - MongoDB can store blogs, media and comments for social media platforms. "Metadata and Asset Management" in MongoDB proposes a model for developing a website content management system.
     
  2. Product Data Management - MongoDB can store information for e-commerce websites and product data management and solutions since it offers a flexible schema that is well suited for the job. The Product Catalog document can also maintain a product catalogue and learn modelling principles and methods.
     
  3. Operational Intelligence - MongoDB is used for real-time analytics and operational intelligence.
     
  4. Mobility and Scaling - MongoDB is a scalable and flexible database that provides excellent database solutions for various scenarios. MongoDB schemas will evolve, allowing mobile app developers to focus their efforts on creating improved client experiences. This will aid many firms in dealing with varied data structures that come from various sources in the mobile app development sector.
     
  5. Real-Time Data Integration - MongoDB allows users to access aggregated data in a single view in real-time, which gives immense value to businesses. With MongoDB 3.6, a developer can additionally take immediate action on specific events and monitor them.
     
  6. Product Catalog - Numerous product attributes can be easily stored as an object in MongoDB and leveraged to better understand the client in the digital experience. Data quality and volume are improving these days, and they're being leveraged to boost client interaction in product catalogues.

 

Organizations that use MongoDB

MongoDB is designed for high availability and scalability, with built-in replication and auto-sharding. MongoDB is used in the tech stacks of several Fortune 500 companies. It is used by several well-known companies, including:

 

  1. eBay
  2. Uber
  3. Lyft
  4. MetLife
  5. Shutterfly
  6. Aadhar
  7. Accenture

Frequently Asked Questions

1). What is a Document in MongoDB?

Answer: A Document in MongoDB is an ordered set of keys with associated values. It is represented by a map, hash, or dictionary. 

2).  What is a Collection in MongoDB?

Answer: In MongoDB, a collection is a group of documents. A collection can be considered as the MongoDB version of a table.

3). What are Databases in MongoDB?

Answer: MongoDB categorizes data into databases. MongoDB may have several databases, each including collections.

4). How does Scale-Out occur in MongoDB?

Answer: MongoDB's document-oriented data model makes it simple to distribute data over several servers. MongoDB is in charge of balancing and loading data across a cluster. It then automatically redistributes papers.

The mongos server serves as a query router, connecting client applications to the sharded cluster.

Key Takeaways

In this article, you were introduced to MongoDB, its functional features, and features that make it the most popular non-relational database. 

 

This article is a part of three blog series on MongoDB. The second part of this blog contains tutorials to help you create Databases, Collections and Documents in MongoDB. And the third blog contains CRUD operations in MongoDB. 

 

For a complete understanding of MongoDB, we suggest you read all three articles.

 

Happy Learning!

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think