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­ist­ic of modern web ap­plic­a­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­egor­ized as a NoSQL database system. Instead, MongoDB uses the JavaS­cript-based MongoDB Query Language (MQL) as its query language. Con­sequently, MongoDB records are stored in the BSON format, which is modelled after JSON. This means that all JavaS­cript data types are supported, which is why MongoDB has become the ideal choice for many JavaS­cript-based platforms.

Another char­ac­ter­ist­ic of MongoDB databases is their ability to scale ho­ri­zont­ally. 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­ab­il­ity and improves overall database per­form­ance. 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­pletely 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­defined structure. The number of fields can vary between documents, and fields don’t have to be con­sist­ent with each other. Documents fulfil 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 JavaS­cript, there are already a variety of pre­defined 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­tens­ively doc­u­mented and explained in detail in the doc­u­ment­a­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­res­pond­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­term­ines 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­decim­al number. This number serves as the document’s object ID. If this is not specified when inserting the document, MongoDB will auto­mat­ic­ally generate an ID for the document.

What are the benefits of MongoDB?

The structure of MongoDB offers several benefits for users, es­pe­cially in terms of flex­ib­il­ity and scalab­il­ity. 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­utes. This allows for the storage of un­struc­tured and semi-struc­tured data. Ad­di­tion­ally, in­di­vidu­al 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­ib­il­ity with many commonly used JavaS­cript platforms.

In addition to storing un­struc­tured data, MongoDB also offers a high degree of ho­ri­zont­al scalab­il­ity without going against ACID (atomicity, con­sist­ency, isolation, dur­ab­il­ity) prin­ciples. In dis­trib­uted databases, ensuring con­sist­ency can often be a challenge, because data is stored on different servers. When changes are made to multiple documents sim­ul­tan­eously, these changes may not be im­me­di­ately 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­sist­ent 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­fi­cing con­sist­ency or avail­ab­il­ity.

Tip

Need a document-oriented database that scales with the re­quire­ments of your web ap­plic­a­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­cessing.

The database system also enables ho­ri­zont­al, nearly limitless scalab­il­ity, as databases can be ef­fort­lessly dis­trib­uted across multiple servers without com­prom­ising func­tion­al­ity. 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­ab­il­ity of your data. The document-oriented database program also displays im­press­ive cap­ab­il­it­ies when it comes to ag­greg­at­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­ist­ics:

  • Scalab­il­ity: as your web project grows, the number of requests typically increases, resulting in higher demands on the database.
  • Avail­ab­il­ity: you need un­in­ter­rup­ted ac­cess­ib­il­ity for your web ap­plic­a­tion, even in the event of server failure.
  • Flex­ib­il­ity: your project should allow for dynamic ad­apt­a­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