10 Reasons to Learn MongoDB for 2019
Malini Shukla
Senior Data Scientist || Hiring || 6M+ impressions || Trainer || Top Data Scientist || Speaker || Top content creator on LinkedIn || Tech Evangelist
Why MongoDB?
As it is a NoSQL database, that’s why it has many reasons to learn MongoDB. These reasons have led the foundation to the worldwide popularity of MongoDB.
These are some reasons, of why MongoDB is popular.
- Aggregation Framework
- BSON Format
- Sharding
- Ad-hoc Query
- Capped Collection
- Indexing
- File Storage
- Replication
- MongoDB Management Service (MMS)
Do you know How to Install MongoDB?
i) Aggregation framework
We can use it in a very efficient manner by MongoDB. MapReduce can be used for batch processing of data and also for aggregation operations. MapReduce is nothing but a process, in which large datasets will process and generate results with the help of parallel and distributed algorithms on clusters.
It consists of two sets of operations in itself, they are: Map() and Reduce().
- Map(): It performs operations like filtering the data and then performing sorting on that dataset.
- Reduce(): It performs the operation of summarizing all the data after the map() operation.
ii) BSON format
It is JSON-like storage a format. BSON stands for Binary JSON. BSON is binary-encoded serialization of JSON like documents and MongoDB uses it, when to stores documents in collections. We can add data types like date and binary (JSON doesn’t support).
BSON format makes use of _id as a primary key over here. As stated that _id is being used as a primary key so it is having a unique value associated with itself called as ObjectId, which is either generated by application driver or MongoDB service. Below is an example to understand BSON format in a more better way:
Example-
- {
- "_id": ObjectId("12e6789f4b01d67d71da3211"),
- "title": "Key features Of MongoDB",
- "comments": [
- ...
- ]
- }
Another advantage of using BSON format is that it enables to internally index and map document properties. As it is designed to be more efficient in size and speed, it increases the read/write throughput of MongoDB.
iii. Sharding
The major problem with any web/mobile application is scaling. To overcome this MongoDB has added sharding feature. It is a method in which, data is being distributed across multiple machines. Horizontal scalability is being provided with the sharding.
It is a complicated process and is done with the help of several shards. Each shard holds some part of data and functions as a separate database. Merging all the shards together forms a single logical database. Operations over here are being performed by query routers.
iv. Ad hoc queries
MongoDB supports range query, regular expression and many more types of searches. Queries include user-defined Javascript functions and it can also return specific fields from the documents. MongoDB can support ad hoc queries by using a unique query language or by indexing BSON documents.
Let’s see the difference between SQL SELECT query and resembling query:
E.g. Fetching all records of student table with student name like ABC.
- SQL Statement – SELECT * FROM Students WHERE stud_name LIKE ‘%ABC%’;
- MongoDB Query – db.Students.find({stud_name:/ABC/ });
v. Schema-Less
As it is a schema-less database(written in C++), it is much more flexible than the traditional database. Due to this, the data does not require much to set up for itself and reduced friction with OOP. If you want to save an object, then just serialize it to JSON and send it to MongoDB.
vi. Capped Collections
MongoDB supports capped collection, as it is having fixed size of collections in it. It maintains the insertion order. Once the limit is reached it starts behaving like a circular queue.
Example – Limiting our capped collection to 2MB
- db.createCollection(’logs’, {capped: true, size: 2097152})
viii. Indexing
To improve the performance of searches indexes are being created. We can index any field in MongoDB document either primary or secondary.
Due to this reason, the database engine can efficiently resolve queries.
viii. File Storage
MongoDB can also be used as a file storing system, which avoids load imbalance and also data replication. This function performed with the help of Grid File System, it is included in drivers which stores files.
ix. Replication
Replication is being provided by distributing data across different machines. It can have one primary node and more than one secondary nodes in it (replica set). This set acts like a master-slave. Here, a master can perform read and write and a slave copies data from a master as a backup only for a read operation.