What is MongoDB used for?
Companies and projects of all sizes use MongoDB across the globe. From weekend-long hackathon projects to large banking systems, MongoDB is the most preferred choice of database.
This is primarily because of the global and widespread data centres of MongoDB and also the free community edition along with the amazing community support forum. Some of the wide areas where MongoDB is used as a database are:
- Quick and Iterative Mobile and Web app development
- Analytics based on the data stored in the database
- Internet of Things
- High-consistency and near-zero downtime requirement systems like payments and banks
MongoDB has been in the industry for years and improved over time proving a plethora of features that contribute to a productive and efficient development cycle.
Some of the companies that use MongoDB include Forbes, KPMG, Royal Bank of Scotland to name a few.
What is MongoDB
MongoDB is a highly flexible database that can suit a hackathon project and even large scale complicated systems like a bank.
MongoDB takes the best of both worlds - relational databases and NoSQL databases. It provides an architecture that suits varying complexity of applications, ranging from mini projects to full fledged large scale systems. MongoDB databases are built around a JSON like a document structure that is both intuitive and flexible for developers to build. It provides developer productivity and quick iteration for changing needs of the application.
Learn to implement database layer for a food-ordering app using MongoDB in Crio's project-based Backend Development Track.
An analogy should help you get a better understanding of the topic if you are new to databases.
If you are new to databases
Consider the scenario of a school wherein you want to store the data of students, teachers and other related information in a systematic manner.
Assuming a world where you have a hard-copy record of the data, one of the ways to store would be to have a folder consisting of sheets of paper, where each sheet contains data of individual students. Similarly, we can have a separate folder to store data about the teachers of the school.
If the school initially did not have a bus service for students and now starts it, we can easily add that information to the sheet of paper of all the students who availed the bus service, without much hassle.
Mongo database is pretty similar with a highly flexible schema owing to its NoSQL nature. Here is a tabular mapping of the above example to its equivalent in MongoDB.
If you have prior experience with SQL databases
MongoDB stores data in a document model, as compared to storing in rows in SQL.
Individual documents are not restricted to have the same schema, enabling a flexible design where required.
The following table summarizes the MongoDB equivalent of the SQL database.
Although MongoDB is a highly versatile database that offers nearly all features and even more than that an SQL database provides, it is recommended not to draw direct parallels between the NoSQL MongoDB and SQL databases.
Some of the best practices and conventions in MongoDB might seem counter-intuitive, hence directly applying concepts from SQL might not always work the best.
How is MongoDB different from SQL?
MongoDB is a combination of the best of the traditional SQL databases and NoSQL databases, making it one of the best of both worlds.
MongoDB stores data in document-model in BSON format.
BSON is a superset of JSON that has additional capabilities like some new data structures that are not natively supported in JSON.
Although data is stored in BSON format, whenever a query is made, the response is always converted into JSON to make it human-readable and also easy to use in APIs.
Some of the prominent features that set MongoDB apart are listed in the following section.
MongoDB has a highly flexible schema. A flexible schema eases out the iteration cycles by allowing you to make quick changes as and when requirements change.
Read operations in NoSQL databases are usually faster than SQL databases because data in SQL databases are typically normalized.
Normalised data can lead to querying multiple tables just to get a single read operation. Data is stored in a manner that optimises it for read operations in NoSQL databases.
Schema is built for NoSQL databases in a format that optimises it depending on the queries required by the application.
This is done based on statistical data that almost all applications have higher read operations than write, hence the database is read-optimised more than write.
Write large dataset without failure
When writing multiple documents to the database, one possible scenario is a partial failure. MongoDB eliminates it by leveraging transactions.
For situations that require atomicity(there is no partial fail, either complete success or complete failure) of multiple documents (in single or multiple collections), MongoDB supports multi-document transactions.
ACID (Atomicity, Consistency, Isolation, and Durability) compliant transactions make it easier for developers to build complex applications with MongoDB.
By design, operations on a singular document are atomic in MongoDB. Because data is generally stored for optimising read operations, by having data that's read together stored together, this single document atomicity is usually sufficient for many use cases.
Join data across collections
This is a really powerful tool to query over multiple collections and is the SQL equivalent of joins.
Aggregation operations take documents as input and do computation to provide computed results. These operations compute from a group of values from multiple documents together.
An example use-case would be to get the average order value of a customer in an e-commerce application by combining data of user’s purchase and item’s value.
Build a scalable NodeJS backend for a shopping app using Express framework and MongoDB database in Crio's project-based Full Stack Development Track.
Powerful Indexing and Search
Indexing is like having a table of content for the database. It improves the query response time.
MongoDB's Atlas Search provides powerful text indexing that enables advanced search functionality without the need of any additional requirement.
The powerful search can auto-correct common words for spelling errors and search across the entire collection of multiple fields and return the result needed.
As an example, MongoDB can find documents with the word ‘Titanic’ even if you misspelt it as ‘Titanoc’.
File Storage directly in MongoDB
Although it is highly unconventional to store files directly in the database, MongoDB has its separate filesystem to store files in the database itself in an optimised way without any hassle or performance issues.
MongoDB uses its specification known as GridFS for storing and fetching files. It divides a file into small divisions and stores each set of data in a separate document within the same collection.
This comes in handy where having a dedicated file storage solution is not possible due to economical or application constraints.
Analytics and Availability in MongoDB Atlas
MongoDB Atlas is a cloud database service on the market, with unmatched data distribution and mobility across AWS, Azure, and Google Cloud provided by MongoDB.
Collections in Atlas support Analytics by default that developers can leverage to understand where the database is loaded and optimise it accordingly.
With the database service globally distributed across major cloud providers, it also provides high availability with near-zero downtime with backup servers.
Setting up MongoDB
MongoDB can be set up for a project in three primary ways:
- MongoDB Atlas: Database on the cloud, managed by MongoDB
- Self-hosted MongoDB Community Server: Database on own server, fully managed by you
- Enterprise Server: Available as part of the MongoDB Enterprise Advanced subscription. It features a complete suite of features including direct support and on-site technical assistance.
MongoDB Atlas has a free tier that is more than sufficient for any project with low to medium complexity. Additionally, for students, it offers USD 200 worth of Atlas credits as part of GitHub Education that can be claimed for free by students.
Try out this cool project to deepen your understanding of MongoDB and master full stack development skills.
Other offerings of MongoDB
MongoDB as of now is not just a database, but a well-established company. Another offering that may be useful for developers is MongoDB Realm. It is the server-less equivalent of MongoDB that converts databases directly into API for direct consumption by client apps. With a very profound experience in the database world, MongoDB definitely is the go-to database for any project.