In the previous article, you have read about what is MongoDB and what is a document-oriented database and NoSQL database. In this article, you will learn about some key features of MongoDB. There are many awesome features that have made MongoDB so popular. Basically, MongoDB was introduced to overcome relational databases approach and limitations of other NoSQL solutions. And, MongoDB has brought in some more, very important and useful features.
Top 10 Key Features Of MongoDB
There are many key features of MongoDB that make it a preferred database when approaching modern web application developments. Below are the major key features of MongoDB:
1) Aggregation Framework
MongoDB provides aggregation feature to use it in an efficient manner. For batch processing of data and aggregation operations, MapReduce can be used. MapReduce is nothing but an associated implementation for processing and generating big data sets with the parallel, distributed algorithm on a cluster.
A MapReduce is a programming model composed of two procedures: Map() and Reduce().
- Map() procedure performs filtering and sorting
- Reduce() procedure performs a summary operation
The aggregation framework enables users to obtain the kind of results for which the SQL GROUP BY clause is used. This MapReduce can be used to parallelize, allow huge data for processing over lots of cores/machines.
2) MongoDB uses BSON format
One of the key features of MongoDB is that it uses BSON format. BSON is a JSON-like storage format. BSON stands for Binary JSON which is a binary-encoded serialization of JSON-like documents that MongoDB uses when storing documents in collections. It adds support for data types like Date and binary that aren’t supported in JSON. BSON format is the use of the _id field as primary key. The value of _id field will usually be a unique identifier type, named ObjectId, that is either generated by the application driver or by the mongod service (in case application driver could not generate this). Below is the sample BSON representation:
{ "_id": ObjectId("12e6789f4b01d67d71da3211"), "title": "Key features Of MongoDB", "comments": [ ... ] }
Another advantage of using BSON format is that it enables MongoDB to internally index and map document properties and even nested documents. It is designed to be more efficient in size and speed, allowing MongoDB’s high read/write throughput.
3) MongoDB Sharding
The major and very common problem with a growing web application is scaling. To overcome this, MongoDB has come up with Sharding feature. It is one of the greatest key features of MongoDB. Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations. Sharding makes it possible to provide horizontal scalability. Horizontal scaling (referred as sharding also) is a complicated process and is done using several machines or shards. Each shard holds a portion of the data and functions as a separate database. The collection of several shards together is what forms a single logical database. The operations are performed through services called query routers, App server and configuration servers which decide which operation needs to be routed to which shard. Thus, it helps in load balancing too. However, it is not a simple task since we need to properly divide the data between different machines and manage the read/write operations between them.
Sharding Infrastructure
We will learn more about Sharding in the upcoming articles.
4) MongoDB Ad hoc queries
MongoDB supports field, range queries, regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions. MongoDB is able to support ad hoc queries by indexing BSON documents and using a unique query language.
Let’s take an example from SQL SELECT statement and corresponding MongoDB query:
E.g. Fetching all the records of Employees table with Employee name like MOHIT.
SQL statement:
SELECT * FROM Employees WHERE emp_name LIKE '%MOHIT%';
MongoDB Query:
db.Employees.find({emp_name:/MOHIT/ });
Also Read: MongoDB Find Command to Query The Documents
5) MongoDB is Schema – Less
MongoDB is a schema-less database (written in C++) because of which is much more flexible than traditional database tables. The benefit is the lack of setup and the reduced friction with OOP. So, in order to save an object, you just have to serialize it to JSON and send it to MongoDB. There is no need for type mapping which removes an additional burden.
6) Capped Collections
MongoDB supports fixed-size collections called capped collections. This type of collection maintains insertion order. Once the specified size has been reached, it starts behaving like a circular queue.
To create a capped collection we can use the db.createCollection command:
E.g. Limiting our capped collections to 2 MB
db.createCollection(’logs’, {capped: true, size: 2097152})
7) MongoDB Indexing
Indexes are created to improve the performance of searches. The good thing is that any field in a MongoDB document can be indexed with primary and secondary indices. It enables the database engine to efficiently resolve queries which make it one of the best key features of MongoDB. The database engine can use a predefined index, which maps documents fields and can tell the engine which documents are compatible with this query statement, hence improves performance.
Also Read: MongoDB Index Tutorial
8) File storage
MongoDB can be used as a file system with load balancing and data replication features over multiple machines for storing files. This function, called Grid File System, is included with MongoDB drivers which stores files. MongoDB exposes functions for file manipulation and content to developers.
9) Replication
MongoDB provides replication feature by distributing data across different machines. It can have one primary node and one or more secondary nodes. This typology is known as replica set. Replica set is like master-slave replication. A master can perform Reads and Writes and a Slave copies data from the master and can only be used for reads or back up (not writes). Another robust feature of the MongoDB replica set is its automatic failover. When one of the set members can’t reach the primary instance for more than 10 seconds, the replica set will automatically elect and promote a secondary instance as the new primary. When the old primary comes back online, it will rejoin the replica set as a secondary instance. Thus, MongoDB provides high availability. Replication is one of the major key features of MongoDB that make it production-ready.
10) MongoDB Management Service (MMS)
MMS is a powerful web tool that allows us tracking our databases and our machines and also backing up our data. MMS also tracks hardware metrics for managing a MongoDB deployment. It shows performance in a rich web console to help you optimize your deployment. It also provides features of custom alerts which helps to discover issues before your MongoDB instance will be affected.
Some Other Key Features of MongoDB
- MongoDB supports multiple storage engines, such as WiredTiger Storage Engine and MMAPv1 Storage Engine. Storage Engines manages how data is saved in memory and on disk. MongoDB also provides pluggable storage engine API that allows third parties to develop storage engines for MongoDB.
- MongoDB supports for geospatial indexes. It allows users to store x and y coordinates within documents. To find the documents which are $within a radius or $near a set of coordinates. (To find documents, we use MongoDB Find Command)
- MongoDB has a rich Query Language, supporting all the major CRUD operations. The Query Language also provides good Text Search features.
That’s all in this article. This is a brief overview of MongoDB features. We will explore more in detail in the later articles. What according to you should also be included in the key features of MongoDB? Do share your views in the comment section below.
- MongoDB Operators Tutorial – What are Different Operators Available? - October 5, 2019
- MongoDB Projection Tutorial : Return Specific Fields From Query - March 9, 2019
- MongoDB Index Tutorial – Create Index & MongoDB Index Types - July 6, 2018