Re­la­tion­al databases were the go-to solution for managing data for decades. However, the growing volume and diversity of data that is char­ac­ter­is­tic of modern web ap­pli­ca­tions has made document-oriented databases like Mongo DB a serious contender. We’ll explain what the document-oriented approach is and the benefits that this data man­age­ment model offers.

How is MongoDB different from other databases?

In tra­di­tion­al re­la­tion­al databases, data is stored in a struc­tured table. MongoDB, which derives its name from the word humongous, is a document-oriented database system. In MongoDB, data is grouped together in an un­struc­tured manner in what the database program refers to as col­lec­tions.

Another key dif­fer­ence is that MongoDB doesn’t use SQL as a query language, und as such, is cat­e­go­rized as a NoSQL database system. Instead, MongoDB uses the JavaScript-based MongoDB Query Language (MQL) as its query language. Con­se­quent­ly, MongoDB records are stored in the BSON format, which is modeled after JSON. This means that all JavaScript data types are supported, which is why MongoDB has become the ideal choice for many JavaScript-based platforms.

Another char­ac­ter­is­tic of MongoDB databases is their ability to scale hor­i­zon­tal­ly. This means that the data in a database can be spread over multiple servers instead of being stored in one central location. This approach allows for increased data avail­abil­i­ty and improves overall database per­for­mance. Horizonal scaling is often more cost-effective than upgrading a single server with more powerful hardware (vertical scaling).

How does MongoDB work?

In contrast to MySQL, MongoDB takes a com­plete­ly different approach to storing data and works in a document-oriented fashion. Instead of storing data in a tabular manner, where each row has the same number of fields that need to be filled with values, MongoDB databases store data in BSON documents that don’t have any pre­de­fined structure. The number of fields can vary between documents, and fields don’t have to be con­sis­tent with each other. Documents fulfill a role similar to that of rows in an SQL table, while the fields that have been defined in these documents serve as columns.

There are many tools that you can use when working with MongoDB databases. In addition to the trusted MongoDB Shell, you can also use the GUI MongoDB Compass to manage MongoDB databases. In this article, however, we’ll focus on Shell commands.

Since MQL is based on JavaScript, there are already a variety of pre­de­fined functions that you can use for complex queries and commands. For the most part, all write and read commands have the same structure as the commands presented in the example below. In addition to these commands, there are numerous other commands that you can use to manage your MongoDB database. These are ex­ten­sive­ly doc­u­ment­ed and explained in detail in the doc­u­men­ta­tion for MongoDB

> db.students.find(Name : "Charlie")
> db.students.updateOne(Name : "Charlie", Semester : 2)
> db.students.deleteOne(Name : "Peter")
> db.students.insertOne({
Name : "Elsie"
Age : 18
Semester : 1
})

In the example above, there is a series of MongoDB database commands, which are all struc­tured in the same way. First, there is “db”, which refers to the database in its entirety, then “students”, which refers to a specific col­lec­tion, where the cor­re­spond­ing op­er­a­tions should be carried out. After the col­lec­tion comes the method (find, update, remove, insert) that should be used.

You can use the find() method to search for a specific document. If you leave the parameter field empty, it will return all the documents in the col­lec­tion you have selected. In the example above, MongoDB searches for all documents where the “Name” field has the value “Charlie”.

The updateOne() method can be used to overwrite values in a document. The update() method takes two arguments. The first argument is the selection criteria, and the second one is the key-value pair to be over­writ­ten. In the example, all students named “Charlie” are selected. The value “2” is then entered in the “Semester” field for students whose name is Charlie.

The deleteOne() method only takes one argument. This is the selection criterion that de­ter­mines which documents should be removed from the database. Here, all students with the name Peter are removed from the database.

The insertOne() method also takes an argument. With this method, the argument is the JSON content of the document that you want to add. When inserting documents, it’s crucial to ensure that each document in the MongoDB col­lec­tion has a unique 12-byte hexa­dec­i­mal number. This number serves as the document’s object ID. If this is not specified when inserting the document, MongoDB will au­to­mat­i­cal­ly generate an ID for the document.

What are the benefits of MongoDB?

The structure of MongoDB offers several benefits for users, es­pe­cial­ly in terms of flex­i­bil­i­ty and scal­a­bil­i­ty. One key advantage of document-based data storage is that, in contrast to re­la­tion­al databases, not all entries need to have the same at­trib­ut­es. This allows for the storage of un­struc­tured and semi-struc­tured data. Ad­di­tion­al­ly, in­di­vid­ual documents can be re­struc­tured (for example, by adding or deleting an attribute/field) without needing to re­struc­ture other documents in the same col­lec­tion. Since documents in MongoDB are stored in BSON format, MongoDB databases also provide a high degree of com­pat­i­bil­i­ty with many commonly used JavaScript platforms.

In addition to storing un­struc­tured data, MongoDB also offers a high degree of hor­i­zon­tal scal­a­bil­i­ty without going against ACID (atomicity, con­sis­ten­cy, isolation, dura­bil­i­ty) prin­ci­ples. In dis­trib­uted databases, ensuring con­sis­ten­cy can often be a challenge, because data is stored on different servers. When changes are made to multiple documents si­mul­ta­ne­ous­ly, these changes may not be im­me­di­ate­ly dis­trib­uted to all the servers that the database system is using. If there is a high volume of queries, this can sometimes lead to in­con­sis­tent data. However, with the release of MongoDB 4.2 in 2019, it’s now possible to make changes to multiple documents on different servers without sac­ri­fic­ing con­sis­ten­cy or avail­abil­i­ty.

Tip

Need a document-oriented database that scales with the re­quire­ments of your web ap­pli­ca­tion? MongoDB is the perfect solution!

What can MongoDB be used for?

MongoDB is an excellent choice for web projects that rely on extremely large un­struc­tured datasets. Working with a document-based system that doesn’t have a fixed schema is ideal for handling a wide range of data types that require rapid storage and pro­cess­ing.

The database system also enables hor­i­zon­tal, nearly limitless scal­a­bil­i­ty, as databases can be ef­fort­less­ly dis­trib­uted across multiple servers without com­pro­mis­ing func­tion­al­i­ty. Moreover, with MongoDB, you can easily create copies of your database and make it available to various servers, ensuring the long-term security and avail­abil­i­ty of your data. The document-oriented database program also displays im­pres­sive ca­pa­bil­i­ties when it comes to ag­gre­gat­ing data, be it from a single source or from multiple sources.

MongoDB is ideal for web projects that exhibit the following char­ac­ter­is­tics:

  • Scal­a­bil­i­ty: As your web project grows, the number of requests typically increases, resulting in higher demands on the database.
  • Avail­abil­i­ty: You need un­in­ter­rupt­ed ac­ces­si­bil­i­ty for your web ap­pli­ca­tion, even in the event of server failure.
  • Flex­i­bil­i­ty: Your project should allow for dynamic adap­ta­tion at all times.

Not sure which database model is the right one for your web project? That’s okay, because you don’t have to choose just one. It’s possible to combine different types of database models, and choosing more than one may be the most suitable choice for your project re­quire­ments.

Tip

So, you’ve decided to go with MongoDB and want to start setting up your database. Our article on how to install and set up MongoDB can help get you started.

Go to Main Menu