Before we talk about the advantages and disadvantages of MongoDB, it is important to know what MongoDB is, and get a basic understanding of its features and functioning. If you don’t know what MongoDB is yet, you might want to start with this post: MongoDB for Beginners – Introduction to MongoDB , then come back here.
What is Mongo DB?
But very briefly, MongoDB is an open source database that uses a customizable data storage scheme (more on that in the post).
It was launched in 2007, and today it is one of the most popular non-relational databases , it was built with scalability, maximum availability and good performance in mind, and is currently (2022) in version 5.0 .
What is the difference between a relational and non-relational database? A NoSQL database stores data differently than a relational database; instead of storing data in tables of rows and columns, all records in a MongoDB database are documents defined in a binary representation of data called BSON. This information is retrieved by applications in JSON format.
NoSQL means ‘Not just SQL’, and there are many types in it, like columns, documents, graphs, key-value pairs, etc.; MongoDB is document-type as mentioned above.
In relational databases, developers need to translate tables to the object model to make them suitable for use in the application; however, with MongoDB, both the stored data and the object model are of the same BSON structure.
Advantages
Greater flexibility with documents
As mentioned before, MongoDB uses documents as its data storage base, which allows virtually any data structure to be modeled and manipulated easily. MongoDB’s BSON data format, inspired by JSON, allows you to have objects in a collection with different sets of fields, for example, if a user is married, we should also store the name of the spouse, if the user is single, that field simply does not need to exist.
{
_id: "a88ea6e3f48",
name: "Fernand Jonhson",
age: 54,
statusMarital: "Married",
occupation: "Caretaker",
spouse: "Thelman" <----------
}
{
_id: "dc64da5578d",
name: "Carl Hop",
age: 32,
statusMarital: "single",
occupation: "Doctor"
}
This flexibility is an incredible advantage when dealing with real-world data and changing business requirements or rules.
Widely supported and native access in code
Most databases force us to use frameworks, wrappers, or heavy tools like ORMs (Object-Relational Mappers) to get data in the form of Objects for use in programs.
MongoDB’s decision to store and represent data in a document format means that you can access it from any language, in data structures native to that language (e.g. dictionaries in Python, objects in JavaScript, Maps in Java, etc).
However, you will need a driver (A library that creates a connection between your application and the MongoDB database), you can check the list of all MongoDB drivers at this link.
In this example below, we can see how you could connect and save new records to a MongoDB database using PHP.
<?php
$client = new MongoDB\Client( "mongodb://localhost:27017" );
$collection = $client->demo->students;
$result = $collection->insertOne( [ 'name' => 'Gustavo' , 'class' => '9A' ] );
echo "New student ID: '{$result->getInsertedId()}'" ;
$result = $collection->insertOne( [ 'name' => 'Tomas' , 'class' => '9C' , 'shift' => 'diurno' ] );
echo "New student ID: '{$result->getInsertedId()}'" ;
?>
In the example, I open a connection to the database called demo
and look for the collection alunos
, in this collection I insert two new records, and as you can notice record 2 has different parameters compared to record 1, which is something totally acceptable with MongoDB.
High performance
Thanks to the document model used in MongoDB, information can be incorporated within a single document instead of relying on JOIN
traditional relational database operations.
For example, in the traditional database model, we would have a table for our customers, and a separate table with the addresses of those customers.
Of course, we can easily reproduce this same model in MongoDB by simply creating two collections, one for customers and one for addresses. And to create the reference between the documents, each address would have a parameter that would be responsible for making the link, in this case the cliente_id
.
// client document
{
_id: "72c4hxrt" ,
name: "Joe Bookreader"
}
// address documents
{
client_id: "72c4hxrt" , // reference to the client document
street: "123 Angel Street" ,
city: "Sao Paulo" ,
state: "SP" ,
postal_code: "200128-120"
}
{
client_id: "72c4hxrt" ,
street: "1 Oliveira Street" ,
city: "Salvador" ,
state: "BA" ,
postal_code: "291021-122"
}
With MongoDB we can simplify and unite all the information in just one document in a much simpler way using the Embedded Document Pattern, in this case, aggregating the customer addresses in the customer document is what makes the most sense.
// client document
{
_id: "72c4hxrt" ,
name: "Joe Bookreader" ,
addresses: [
{
street: "123 Angel Street" ,
city: "Sao Paulo" ,
state: "SP" ,
postal_code: "200128-120"
},
{
street: "1 Oliveira Street" ,
city: "Salvador" ,
state: "BA" ,
postal_code: "291021-122"
}
]
}
This makes queries much faster and returns all the necessary information in a single call to the database. See what the new document would look like with the addresses added.
When it comes to write performance, MongoDB offers functionality to insert and update multiple records at once with insertMany
and updateMany
. These two functions offer a significant performance boost when compared to batch writes in traditional databases.
For our example, where we created two student records in two separate method calls insertOne
, we could have written using the method insertMany
as you can see below.
<?php
$client = new MongoDB\Client( "mongodb://localhost:27017" );
$collection = $client->demo->students;
$result = $collection->insertMany(
[ 'name' => 'Gustavo' , 'class' => '9A' ],
[ 'name' => 'Tomas' , 'class' => '9C' , 'shift' => 'daytime' ]
);
?>
Another great advantage of MongoDB is that it stores most of its data in RAM instead of on the hard drive, which allows for faster performance when executing queries. To ensure faster processing, make sure that your indexes fit entirely in RAM so that the system can avoid reading the index from disk.
MongoDB supports transactions
A transactional database is a database that supports ACID (atomicity, consistency, isolation, and durability) transactions. A transaction is a set of database read and write operations where either all or none of the operations succeed.
Single-document operations have always been atomic in MongoDB. MongoDB added support for multi-document ACID transactions in version 4.0, and expanded that support to include distributed transactions in version 4.2.
The guarantees provided by MongoDB ensure complete isolation while a document is updated; any error causes the operation to be rolled back, returning the document unchanged.
With proper modeling, transactions that span multiple records are not always necessary. Data in MongoDB, as we saw earlier, can be related and modeled into a single data structure using a variety of types, including subdocuments and arrays. By doing so, MongoDB users can achieve the same data integrity guarantees as those provided by relational databases.
Change-friendly structure
If you’re used to having to take down your website or application to change the data structure, you’re in luck: MongoDB was designed for change.
Unlike SQL databases, where you must determine and declare the schema of a table before inserting data, MongoDB collections by default do not require that your documents have the same schema.
This means there is no downtime for changing schemas, you can start writing new data with different structures at any time without interrupting your operations.
This flexibility makes it easy to map documents to an entity or object. Each document can be mapped to the fields of an Object, even if the document has substantial variation from other documents in the collection.
In practice, however, documents in a collection share a similar structure, and you can apply document validation rules to a collection during update and insert operations.
Disadvantages
Like any software, MongoDB also has its drawbacks. The vast majority of them are limitations that can be fixed or improved in the future, but at the moment (2022), some of the points I will mention below could be a problem for those who are thinking of using MongoDB.
BSON document size
The maximum BSON document size is 16 megabytes.
The maximum document size helps ensure that a single document cannot use an excessive amount of RAM or, during transmission, an excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API .
Nesting limit in BSON documents
MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds one level.
To exemplify this limitation:
{
"_id" : 1 ,
"Universe" : {
"Virgo Cluster" : [
{
"Virgo Supercluster" : {
"Local Group" : {
"Milky Way" : {
"Solar System" : [
{
"Earth" : [
{ "Asia" : [ "Countries" , "..." ], "Europe" : [ "Countries" , ".." ], "America" : [ "Countries" , "..." ] }
]
}
]
}
}
}
},
{ "Laniakea Supercluster" : "..." }
]
}
}
Sorting operations with limits
If MongoDB cannot use an index or indexes to sort the fields of a document, MongoDB initiates a blocking data sort operation .
The name refers to a process SORT
where all documents in a collection are read to return an output document, in which case the data flow for that specific query must be blocked.
If MongoDB requires the use of more than 100 megabytes of system memory for the sort operation, MongoDB returns an error unless the query specifies otherwise cursor.allowDiskUse()
(New in MongoDB 4.4).
allowDiskUse()
allows MongoDB to use temporary files on disk to store data that exceeds the 100 megabyte memory limit while processing a sort operation.
db.COLLECTION_NAME.find().sort({KEY: 1 })
MongoDB Key Terms/Concepts
SQL Terms/Concepts | MongoDB Terms/Concepts |
---|---|
database | database |
table | collection |
line | document or BSON document |
column | field |
index | index |
primary_key Specifies a single column or combination of columns as the primary key. | primary_key In MongoDB, the primary key is automatically set to the _id field. |
Deixe um comentário