Introduction, Overview, and History
MongoDB is a powerful, highly scalable, free and open-source NoSQL based database. MongoDB was initially released approximately 9 years ago on 11th of February, 2009 and has since then achieved the position of the leading NoSQL database. The company MongoDB Inc. (New York, United States) maintains and manages the development of MongoDB. They also provide the commercial version of MongoDB which includes support also. The source code of MongoDB is available on Github.
Over the years, MongoDB has become a popular choice of a highly scalable database and it is currently being used as the backend data store of many well-known organizations like IBM, Twitter, Zendesk, Forbes, Facebook, Google, and a gazillion others. MongoDB has also caught the eyes of the open source community and a lot of developers work on various open source projects based on MongoDB. You can download MongoDB here: https://www.mongodb.com/download-center and find MongoDB documentation here: https://docs.mongodb.com/
Applications of MongoDB
MongoDB is widely used across various web applications as the primary data store. One of the most popular web development stacks, the MEAN stack employs MongoDB as the data store (MEAN stands for MongoDB, ExpressJS, AngularJS, and NodeJS).
When and How to use MongoDB?
MongoDB is a document-based data store which means that it stores the information in rather an unstructured format as compared to structured tables like in MySQL or PostgreSQL. This essentially means that the data stored in MongoDB is “schema-less”. Therefore, MongoDB provides a fast and scalable data storage service which makes it a popular choice in performance critical application. Moreover, the fact that MongoDB has been written in C++ makes it even faster as compared to a lot of other databases.
MongoDB should not be used in applications that require table joins simply because it doesn’t support joins (like in SQL). This is attributed to the fact that the data stored in MongoDB is not structured and therefore, performing joins is a highly time-consuming process which may lead to a slow performance.
Let us consider a simple example of a Student model where a student can take up various university courses. Here is how a simple MongoDB based Student model will look like:
Advantages of using MongoDB
As can be seen in the above example that rather than storing Students and Courses in separate database tables as in SQL, here in MongoDB, we store them together in a single “document”. This speeds up the data retrieval process. Most large scale applications like Facebook require this marginal increase in performance owing to the fact that billions of users are using the application and so, even a minor increase in performance may create a large scale impact overall. This is one of the most important advantages of using MongoDB.
Other advantages include:
- Clustering: MongoDB allows sharding of data across the nodes in a cluster so as to ensure that there is no single-point-of-failure in the database server.
- Support for Secondary Indices: MongoDB allows for not just a primary index but also for a secondary index which is important in many applications.
- Caching: MongoDB caches a lot of data so as to allow faster retrieval of the query results.
When not to use MongoDB?
MongoDB is a NoSQL database and as a result, it is not ACID compliant (Atomicity, Consistency, Isolation, Durability). As a result, in the applications where ACID compliance (for example, applications that require database-level transactions) is required, MongoDB cannot be used. For instance, one might not want to use MongoDB when designing a core-banking system for a bank.
Further, unlike many databases, MongoDB doesn’t have the provision for stored procedures.
Exact performance of the database varies on a lot of factors. For instance:
- Use-case: some applications require data to be structured. In such situations, it is better to use SQL based databases. In other applications, it is next to impossible to structure the data. MongoDB works best here.
- Storage: naturally, the performance (reads and writes per second) will depend heavily on the underlying storage mechanism. For instance, SSDs are certainly faster than typical hard-disks and so, the performance is better with SSDs.
- RAM: database performance greatly depends on caching and the amount of information that can be cached depends on the RAM that is made available to the database.
- Clustering: MongoDB can be used to easily set up a database cluster with high availability and high performance. This is generally difficult in SQL based databases.
MongoDB performs brilliantly in applications that require data to be unstructured. For instance, various MapReduce Applications, Big-Data systems, Social Networking applications, News Forums, etc. In such use-cases, it is difficult to structure data across tables. Even if it is possible to structure data across tables, it is not always desirable because, in queries that require database joins, there is a huge hit on the performance of the database.
How to learn using MongoDB?
The best way to learn MongoDB is to actually develop a web application that uses MongoDB as the backend database. Databases are generally coupled with a web application (rather than being used independently) and therefore you are advised to try the following simple web application ideas:
- A social networking site where users can share posts, like each other’s posts and comment on each other’s posts.
- A question-answer based discussion forum supporting multiple topics.
You can use any web application framework of your choice. Some of the recommended ones are:
- Django: Django is a Python-based web application framework and can be used seamlessly with MongoDB as the backend database. Here is a tutorial for the same.
To summarize, MongoDB is a fast and reliable database which is one of the recommended databases in designing scalable web applications that required storage of unstructured data. If you are designing an application where you need high availability, clustering, fast performance, then you must consider using MongoDB as your application’s backend database. You can check out programming community-recommended best MongoDB tutorials here: https://hackr.io/tutorials/learn-mongodb
People Also Reading: