MongoDB

MongoDB: all about the document-oriented NoSQL database

Margalith

Margalith

4 min

MongoDB is a document-oriented NoSQL database. It differs from relational databases in its flexibility and performance. Find out everything you need to know about this must-have tool for data engineering.

MongoDB is a document-oriented NoSQL database that appeared in the mid-2000s. It is used for storing massive volumes of data.

Unlike a traditional SQL relational database, MongoDB does not rely on tables and columns. Data is stored as collections and documents.

Documents are value/key pairs that serve as the basic data unit. Collections contain sets of documents and functions. They are the equivalent of tables in classical relational databases.

The characteristics of MongoDB

Each MongoDB database contains collections, themselves containing documents. Each document is different and can have a variable number of fields. The size and content of each document also vary.

The structure of a document corresponds to the way developers build their classes and objects in the programming language used. In general, classes are not rows and columns but have a clear structure consisting of value/key pairs.

Documents do not have a predefined schema and fields can be added at will. The data model available within MongoDB makes it easier to represent hierarchical relationships or other complex structures.

Another major feature of MongoDB is the elasticity of its environments. Many companies have clusters of over 100 nodes for databases containing millions of documents.

The MongoDB architecture and its components

The MongoDB architecture is based on several main components. First, “_id” is a required field for each document. It represents a unique value and can be considered as the main key of the document to identify it within the collection. 

A document is the equivalent of a record in a traditional database. It consists of name and value fields. Each field is an association between a name and a value and is similar to a column in a relational database.

A collection is a group of MongoDB documents, and corresponds to a table created with any other RDMS like Oracle or MS SQL on a relational database. It has no predefined structure.

A database is a container of collections, just as an RDMS is a container of tables for relational databases. Each has its own set of files on the file system. A MongoDB server can store multiple databases.

Finally, JSON (JavaScript Object Notation) is a plain text format for expressing structured data. It is supported by many programming languages.

Why use MongoDB ? What are the advantages ?

MongoDB has several major advantages. First of all, this document-oriented NoSQL database is very flexible and adapted to the concrete use cases of an enterprise.

Ad hoc queries allow you to find specific fields within documents. It is also possible to create indexes to improve search performance. Any field can be indexed.

Another advantage is the ability to create “replica sets” consisting of two or more MongoDB instances. Each member can act as a secondary or primary replica at any time.

The primary replica is the main server, which interacts with the client and performs all read and write operations. The secondary replicas keep a copy of the data. Thus, in case of failure of the primary replica, the switchover to the secondary is done automatically. This system guarantees high availability.

Finally, the concept of sharding allows for horizontal scaling by distributing the data among multiple MongoDB instances. The database can be run on multiple servers, and this allows load balancing or duplicating data to keep the system functional in case of hardware failure.

Because of these many advantages, MongoDB is now a widely used tool in the field of data engineering. It is a must-have solution for Data Engineers.

MongoDB vs RDBMS: what are the differences ?

There are several major differences between MongoDB and RDBMS (relational database management system). As mentioned before, data is not stored in tables but in document collections. These documents replace the rows of RDBMS. They contain fields of value/key pairs, which themselves replace columns.

Furthermore, data integrity is not a constraint on MongoDB. Data does not need to be “normalized” before use like on an RDBMS. This is a real advantage, as the normalization constraint can degrade performance as the database grows.

Data modeling on MongoDB

Unlike SQL databases, MongoDB does not involve any constraints in terms of document structure. Data has no preconceived schema, and it is this flexibility that makes MongoDB so powerful and efficient.

The data modeling and document structure must only meet the needs of the user. It is important to consider the needs of the application, and therefore what data and data types will be needed.

If many queries are to be expected, it is relevant to use indexes in the data model to improve the efficiency of queries. Finally, if there are frequent additions, updates and deletions of data, indexes and the sharding system should be used to improve the overall efficiency of the environment.

Why and how to learn to use MongoDB ?

MongoDB is one of the indispensable tools for data engineering. To learn how to use it, you can turn to DataScientest training courses.

The Data Engineer training will teach you the job of a data engineer, and in particular how to build data acquisition and automatic processing pipelines. In the “database” module, you will learn to use MongoDB, but also Cassandra, Elastic Search, Neo4J and the SQL language.

If you are already a Data Scientist and want to learn how to put Machine Learning models into production, you can turn to our Machine Learning Engineer training. MongoDB is one of the tools you will learn to use.

You know all about MongoDB. Learn about other data engineering tools, like the Docker containerization platform or Snowflake’s Cloud Data Warehouse.