MongoDB

Introduction TO MONGODB and the NOSQL concept

MongoDB (1) is a document-oriented database management system. It is part of the NoSQL database family. NoSQL databases deviate from the principle of classic relational databases (DBMS) where a predefined data schema is necessary. In a DBMS, information is stored in tables where the schema imposes columns data type. Therefore, all records of a table must have the same format. The schema also defines relationships between tables (e.g. the second column of the first table corresponds to the entries of the third column of the second table). These relations make it possible to perform a join operation between 2 tables to form a single table.

Documents in MongoDB do not require a predefined schema unlike relational databases. Documents are stored in JSON format (in gray in Figure 1) in a collection (in blue). A collection can have an unlimited number of documents. The collections are themselves integrated into databases (in green) in the MongoDB system.

Figure 1 : MongoDB architecture

Documents

A document is a dictionary in the form of key:value (see Figure 2). A key can be an integer or a string while the value can be an integer, a string, a list of integers, strings, a list of lists but also a dictionary which itself contains dictionaries …

Figure 2 : example of JSON document stored in a MongoDB database

Documents are indexed and can be quickly found by their unique key ‘_id’. The other keys are free and should not be redundant from one document to another in the same collection. They can also be indexed to be quickly found by the system. We therefore have a system with great freedom in the stored data. At the same time, it moves the problem of managing the existence and the type of information available in the documents to the application side that query the database. MongoDB does not allow to make complex queries from the join between documents of 2 collections. These operations must also be performed by the application.

Use cases examples

Content management

Blogs or audio/video content platforms are very suitable applications for using MongoDB. Articles or media can be stored as single documents containing all informations. When modifying the application, only the documents concerned must be updated without interrupting access to the other documents.

Large catalog of heterogeneous data

For example in e-commerce applications, many different products with different attributes could  coexist in the same database. In a relational database, managing thousands of different attributes when only a few are used for a product will not be efficient in terms of database size, writing and reading time. The use of a document-oriented database like MongoDB allows to store only what is strictly necessary. Modifying the attributes of a document has no effect on the other documents in the collection.

Popularity

According to the site https://db-engines.com/, MongoDB occupies the 5th place of the most popular systems in March 2023. The first four places are occupied by historically very used relational databases.

References

  1. https://www.mongodb.com/