MongoDB is a general-purpose, distributed, document-based NoSQL database.
SQL databases are still the vast majority. But with the exponential increase in data consumption and creation, we need to prepare for what is to come. And NoSQL databases have proven to be an excellent alternative for this.
In this post we will better understand what NoSQL databases are and what their advantages are compared to traditional relational databases.
But before we dive right into talking about MongoDB , let’s give you an introduction to NoSQL databases.
A comparison between relational databases and non-relational databases can be found in this other post .
What is a NoSQL database?
NoSQL databases (also known as “not just SQL”) are not tabular, and store data differently than relational tables.
In relational databases you need to create the tables, define the schema, define the data field types, define the relationships between the tables, etc., before you can actually insert data.
In NoSQL databases you don’t have to worry about this. The document creation process, in the case of MongoDB, is much simpler and more dynamic.
NoSQL databases come in a variety of flavors based on their data model. The main flavors are documents , key-value , wide-column , and graph . They provide flexible and scalable schemas and support high user loads.
One of the advantages of NoSQL databases is that they are really easy to scale and are much faster in most types of operations that we perform on the database.
There are certain situations where using a relational database might be a better choice over NoSQL, but when you are dealing with a large amount of data, a NoSQL database might be the better choice.
Advantages of NoSQL
Today, the advantages of NoSQL databases are no secret, especially as cloud computing has gained widespread adoption.
NoSQL databases were created in response to the limitations of traditional relational database technology. Compared to relational databases, NoSQL databases are more scalable and offer superior performance, and their data model addresses several shortcomings of the relational model.
The advantages of NoSQL include, but are not limited to:
- Support for large volumes of structured, semi-structured and unstructured data.
- Ease of dealing with changes over time.
- Ability to scale horizontally on ‘commodity’ hardware.
- Efficient and scalable architecture instead of expensive and monolithic architecture.
- Support for multiple data structures.
Introduction to MongoDB
MongoDB is a document-oriented NoSQL database used to store large volumes of data. Instead of using tables and rows like traditional relational databases, MongoDB uses collections and documents.
What is document-based storage?
Documents consist of key-value pairs that are the basic unit of data in MongoDB. Collections contain sets of documents and functions that are equivalent to tables in relational databases.
If you come from a relational database, you can think of documents as the rows of the relational database and collections as the tables.

{ "_id" : "5" , "isActive" : false , "age" : 24 , "eyeColor" : "brown" , "name" : "Ada Compton" }
This is a JSON- like structure , where data is stored in the form of key-value pairs.
{ "_id" : "5" , "isActive" : false , "age" : 24 , "eyeColor" : "brown" , "name" : "Ada Compton" } { "_id" : "6" , "isActive" : false , "age" : 43 , "eyeColor" : "blue" , "name" : "Andray Conway” }
And a collection is nothing more than a set of documents that do not necessarily need to have the same fields, as in the example of the collection below:

What is the difference between MongoDB and RDBMS?
The biggest difference between MongoDB and SQL databases is the way they handle data. In SQL databases, data is stored in a traditional two-dimensional row-column structure, while MongoDB uses documents to store information, which allows it to store any type of data.
Let’s take a look at some of the main differences between MongoDB and other SQL databases:
Uses Tables with rows and columns. | Base | Uses Documents that consist of key-value pairs. |
RDBMS is a relational database management system | Concept | MongoDB is a document-oriented, non-relational database management system. |
RDBMSs are vertically scalable. Performance increases with increasing RAM. | Scalability | MongoDB supports horizontal scaling through Sharding, distributing data across multiple machines. |
Difficulty storing hierarchical data. | Hierarchy | Have built-in support for storing hierarchical data. |
RDBMS supports complex joins. | Joins | NoSQL databases like MongoDB use denormalized data, so ‘relational’ joins are not supported. |
RDBMS is slower in processing large hierarchical data. | Performance | MongoDB is extremely fast at processing large hierarchical data. |
Follows the ACID, Atomicity, Consistency, Isolation and Durability principles. | Principle | It follows the CAP principle, Consistency, Availability and Partition Tolerance. |
RDBMS uses SQL to query databases. | Query Language | MongoDB uses BSON to query database. |
Vulnerable to SQL Injection attacks. | SQL Injection | SQL injection is not possible. |
The schema needs to be defined in the RDBMS before using a database. | Schema | The schema can be created and accessed dynamically in MongoDB. |
Advantages of MongoDB
MongoDB is schemaless, meaning each document can have its own set of unique fields within a collection. Furthermore, it is distributed and easily scales geographically/horizontally for better performance.
When considering moving to a document-oriented database, you may want to consider the following points:
Access to native data from code
Most databases force you to use heavy mappings, such as ORMs (Object Relational Mappers), to get the data into objects for use in your programs. Like Java’s Hibernate and C#’s Entity Framework .
MongoDB’s decision to store and represent data in a document format using JSON means that you can access it from any language, in data structures native to that language (e.g. dictionaries in Python, associative arrays in JavaScript, Maps in Java, etc.).
Change-friendly structure
If you’re used to having to take down your website or application to change the structure of your data, you’re in luck: MongoDB is designed to accept change much more easily.
There’s no downtime required to change schemas, and you can start writing new data to MongoDB at any time without interrupting your operations.
Powerful queries and analytics
What good is a database if you can’t find things in it? MongoDB is designed to make data easy to access.
MongoDB Query Language (MQL) is a powerful, full-featured language that lets you perform deep queries on documents, and even run complex analytics pipelines with just a few lines of JSON-like MQL.
Easy horizontal scalability
MongoDB is designed from the ground up to be a distributed database. Create clusters with real-time replication, and perform large or high-throughput collections across multiple clusters to sustain performance and scale horizontally.
Conclusion
Document-oriented databases have certain advantages: flexibility (lack of rigid structure), good suitability for modern JavaScript frameworks (direct use of JSON), large data processing and real-time statistics/data analysis.
MongoDB is one of the most widely used NoSQL databases. It is easy to understand. The document query language provides many options and is as powerful as SQL. Unlike relational databases, MongoDB is easy to scale. MongoDB is widely used alongside NodeJS, AngularJS, and ReactJS frameworks. It is a core part of the MEAN and MERN stack.
I hope you enjoyed the content.
We’ll see you next time.
Deixe um comentário