What is MongoDB?

Relational databases were the go-to solution for managing data for decades. However, the growing volume and diversity of data that is characteristic of modern web applications 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 management model offers.

How is MongoDB different from other databases?

In traditional relational databases, data is stored in a structured table. MongoDB, which derives its name from the word humongous, is a document-oriented database system. In MongoDB, data is grouped together in an unstructured manner in what the database program refers to as collections.

Another key difference is that MongoDB doesn’t use SQL as a query language, und as such, is categorized as a NoSQL database system. Instead, MongoDB uses the JavaScript-based MongoDB Query Language (MQL) as its query language. Consequently, MongoDB records are stored in the BSON format, which is modelled 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 characteristic of MongoDB databases is their ability to scale horizontally. 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 availability and improves overall database performance. 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 completely 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 predefined structure. The number of fields can vary between documents, and fields don’t have to be consistent 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 JavaScript, there are already a variety of predefined 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 extensively documented and explained in detail in the documentation 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 structured in the same way. First, there is ‘db’, which refers to the database in its entirety, then ‘students’, which refers to a specific collection, where the corresponding operations should be carried out. After the collection 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 collection 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 overwritten. 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 determines 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 collection has a unique 12-byte hexadecimal number. This number serves as the document’s object ID. If this is not specified when inserting the document, MongoDB will automatically generate an ID for the document.

What are the benefits of MongoDB?

The structure of MongoDB offers several benefits for users, especially in terms of flexibility and scalability. One key advantage of document-based data storage is that, in contrast to relational databases, not all entries need to have the same attributes. This allows for the storage of unstructured and semi-structured data. Additionally, individual documents can be restructured (for example, by adding or deleting an attribute/field) without needing to restructure other documents in the same collection. Since documents in MongoDB are stored in BSON format, MongoDB databases also provide a high degree of compatibility with many commonly used JavaScript platforms.

In addition to storing unstructured data, MongoDB also offers a high degree of horizontal scalability without going against ACID (atomicity, consistency, isolation, durability) principles. In distributed databases, ensuring consistency can often be a challenge, because data is stored on different servers. When changes are made to multiple documents simultaneously, these changes may not be immediately distributed to all the servers that the database system is using. If there is a high volume of queries, this can sometimes lead to inconsistent 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 sacrificing consistency or availability.

Tip

Need a document-oriented database that scales with the requirements of your web application? MongoDB is the perfect solution!

What can MongoDB be used for?

MongoDB is an excellent choice for web projects that rely on extremely large unstructured 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 processing.

The database system also enables horizontal, nearly limitless scalability, as databases can be effortlessly distributed across multiple servers without compromising functionality. Moreover, with MongoDB, you can easily create copies of your database and make it available to various servers, ensuring the long-term security and availability of your data. The document-oriented database program also displays impressive capabilities when it comes to aggregating data, be it from a single source or from multiple sources.

MongoDB is ideal for web projects that exhibit the following characteristics:

  • Scalability: as your web project grows, the number of requests typically increases, resulting in higher demands on the database.
  • Availability: you need uninterrupted accessibility for your web application, even in the event of server failure.
  • Flexibility: your project should allow for dynamic adaptation 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 requirements.

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.

Was this article helpful?
Page top