MongoDB | Why MongoDB is Used

Oh, Another Database. You learn SQL in your college or from online Courses and now you hear MongoDB from your senior and now you are trying to learn it. Don’t worry, if you have a little bit of experience with SQL you can find MongoDB super easy. Here, we are going to discuss MongoDB and Why MongoDB is used.

What is MongoDB

MongoDB was first released in February 2009 and the company behind this Database is MongoDB Inc. Mongo uses JSON format to store the data in the database. MongoDB is also called a NoSQL Database, why?. Before Understanding MongoDB let’s know a little bit about storing the data in SQL.

This is the sample Data storing structure in SQL –

mysql
Geekstocode SQL

As you can see there is a well-created Schema for storing the data and once you created a schema making changes in schema are not good practice like Creating new attribute etc.

Also, adding more child attributes is not allowed in SQL, for this, you need to create a new relation and use a foreign key to join two different relations(tables) or use the JOIN keyword to query.

But you can store the same data in Mongo like this –

mysql JSON

Don’t confuse in Key, value, collection, etc. We will explain each term in detail in this post. The main idea behind the above thing is to create a visualization of Mysql Vs MongoDB. Now, Let’s discuss each and every term used in MongoDB.

Terminology In MongoDB vs MySQL

RDBMS MONGODB
DatabaseDatabase
TableCollection
TupleDocuments
ColumnFields
Geekstocode MYSQL vs MongoDB

Database

The database is a set of collections. Single Mongo Server has multiple databases.

Collection

The Group of documents is known as Collection. It is the same as the Tables in the RDBS but the collection does not have a schema in Mongo as present in RDBMS.

Documents

Documents are a set of key-value pairs, also documents in a single collection may have a different schema.

Below is the example of Document, Collection, Database, Key-Value Pair

mongoDB terminology
MongoDB terminology

Why MongoDB is Used

For Fast Data Retrieval

Mongo stores data in a document format and it is a powerful way to retrieve data in a super-fast manner as compare to table format.

For Scaling DataBase

If your app is growing and many users are using your app you need to expand your database and this is done very efficiently in MongoDB.

To Remove Complex ORM

Using MongoDB removes the complex object-relational mapping (ORM) layer that translates objects in code to relational tables

Pros Of MongoDB

Pros of MongoDB usually depends on the type of application you are developing, but these are some evergreen advantages

Malleable Document Schema

Mongo stores the data in a BSON format which is the same as JSON format. MongoDB allows you to have a different set of fields in the same collection.

For example

{
   "FirstName":"Piyush",
   "LastName":"Jaiswal",
   "Number":12323

},
{
  "FirstName":"Geeks",
  "MiddleName": "To",
  "LastName":"Code"
}

The above collection has two documents and both documents have a different schema.

Powerful Query

MongoDB is designed to make data easy to access, and rarely requires joins or transactions. The MongoDB Query Language (MQL) is a full-featured, powerful language that allows you to query deep into documents, and even perform complex analytics pipelines with just a few lines of JSON-like MQL.

No Complex Join

In RDBMS, you need JOIN operators to join two relations which results in a highly complex query.

More Articles to Read

Advantage And Disadvantage of DBMS

Leave a Comment

Your email address will not be published. Required fields are marked *