How MongoDB works behind the scenes
Published: 2022-03-14 15:20:54

Databases are an essential part of development, but how do they work? In this article, I will show you how a no-SQL database like MongoDB works behind the scenes.

## Index
1. What is a database?
* What is MongoDB?
* SQL vs no-SQL databases
* What is JSON?
* What the hell is BSON?
* What is Mongoose?

## 1. What is a database?

A database is a set of structured information (or data) typically stored electronically in a computer system. Usually, the database is controlled by a database management system (DBMS). Data, the DBMS, and associated applications are referred to as a database system, often abbreviated to database only.

## 2. What is MongoDB?

MongoDB is an open-source, non-relational database management system that processes and stores many types of data using flexible documents rather than tables and rows. MongoDB, being a NoSQL solution, does not need a relational database management system, hence it offers an elastic data storage format that allows users to easily store and query multiple data types. This not only makes database administration easier for developers but also enables a highly scalable environment for cross-platform applications and services.

The basic units of data in MongoDB are documents or groups of documents. These documents, which are formatted as Binary JSON (JavaScript Object Notation), may hold many sorts of data and be transferred across multiple platforms. Because MongoDB has a dynamic schema architecture, users have unprecedented freedom when generating data records, querying document collections using MongoDB aggregation, and analyzing enormous volumes of data.

## 3. SQL vs no-SQL databases

*The following table outlines the key differences between SQL and NoSQL databases.*

| | SQL Databases | NoSQL Databases |
| :- | :- | :- |
| **Data Storage Model** | Tables with fixed rows and columns | Document: JSON documents, Key-value: key-value pairs, Wide-column: tables with rows and dynamic columns, Graph: nodes and edges |
| **Development History**| Developed in the 1970s with a focus on reducing data duplication | Developed in the late 2000s with a focus on scaling and allowing for rapid application change driven by agile and DevOps practices. |
| **Examples** | Oracle, MySQL, Microsoft SQL Server, and PostgreSQL | Document: MongoDB and CouchDB, Key-value: Redis and DynamoDB, Wide-column: Cassandra and HBase, Graph: Neo4j and Amazon Neptune |
| **Primary Purpose** | General purpose | Document: general purpose, Key-value: large amounts of data with simple lookup queries, Wide-column: large amounts of data with predictable query patterns, Graph: analyzing and traversing relationships between connected data |
| **Schemas** | Rigid | Flexible |
| **Scaling** | Vertical (scale-up with a larger server) | Horizontal (scale-out across commodity servers) |
| **Multi-Record ACID Transactions** | Supported |  Most do not support multi-record ACID transactions. However, some—like MongoDB—do.|
| **Joins** | Typically required | Typically not required |
| **Data to Object Mapping** | Requires ORM (object-relational mapping) | Many do not require ORMs. MongoDB documents map directly to data structures in most popular programming languages. |

## What is JSON?
JSON, or JavaScript Object Notation, was developed as part of the JavaScript language in the early 2000s by JavaScript developer Douglas Crockford, however, the format wasn't officially standardized until 2013.

Javascript objects are simple associative containers, where a string key is mapped to a value (which can be a number, string, function, or even another object). Because of this fundamental language feature, JavaScript objects might be expressed in text in a wonderfully simple way:
```json
{
  "_id": "1",
  "numberOfStates": 208,
  "states": ["Italy", "Spain", "French", "Germany", "Canada"]
}
```
JSON began to take on a life of its own when JavaScript became the industry standard for client-side web development. JSON quickly spread beyond the web page and into software everywhere because it is both human- and machine-readable, and it is relatively easy to develop support for in other languages.

JSON shows up in many different cases:

- APIs
- Configuration files
- Log messages
- Database storage

JSON rapidly overtook XML, as it is more difficult to comprehend for humans, far more verbose, and less well suited to describing object structures in current computer languages.

## 5. What the hell is BSON?

#### Why BSON?
MongoDB was created to be the ultimate data platform for modern app development. When it came to data structures, JSON was the first option.

However, JSON has many drawbacks that make it unsuitable for use in databases:
1. JSON is a text-based format, and processing text takes a long time.
* Another database problem is that JSON's readable format is inefficient in terms of space.
* Only a few fundamental data types are supported by JSON.

BSON was created to bridge the gap between making MongoDB JSON-first while still being high-performance and general-purpose. BSON is a binary representation for data storage in JSON format that is designed for speed, space, and flexibility.

#### So Marco, what really is BSON?

BSON stands for "Binary JSON," which is precisely what it was created for. The binary structure of BSON encodes type and length information, making it considerably easier to understand.

BSON has been enhanced since its inception to include various optional non-JSON-native data types, including dates and binary data, without which MongoDB would have been without some essential functionality.

Different sized integers (ints versus longs) or different levels of decimal precision are common in languages that enable any type of complicated mathematics (float, double, decimal128, etc.).

Not only is it useful to be able to describe those distinctions in MongoDB data, but it also enables direct comparisons and computations on data, which simplifies consuming application code.

#### What is the choice of MongoDB?

Although MongoDB stores data in BSON format both internally and across the network, you can still think of it as a JSON database. Anything that can be represented in JSON may be stored natively in MongoDB and retrieved in JSON as well.

#### JSON vs BSON

| | JSON | BSON
| :- | :- | :-|
| **Encoding** | UTF-8 String | Binary | 
| **Data Support** | String, Boolean, Number, Array | String, Boolean, Number (Integer, Float, Long, Decimal128...), Array, Date, Raw Binary | 
| **Readability** | Human and Machine | Machine Only | 

BSON differentiates from JSON in that it allows for the storage of some more complex sorts of data. JavaScript, for example, does not distinguish between integers (round numbers) and floating-point numbers (which have decimal precision to various degrees)

## 6. What is Mongoose ?
Mongoose is a MongoDB Object Data Modeling (ODM) framework written in Node.js. It is analogous to an Object Relational Mapper (ORM) for standard SQL databases, such as SQLAlchemy. Mongoose tries to tackle the problem of allowing developers to impose a specified schema at the application layer. Mongoose, in addition to enforcing a schema, provides several hooks, model validation, and other features targeted at making it simpler to interact with MongoDB.

## Conclusion
Now you know how MongoDB works behind the scenes, in my opinion, it is really important to know what happens behind the code, this is because you can solve faster errors regarding your applications. I hope you like this post and if yes come to check at [my Github page](https://github.com/MoscatelliMarco) and my [new project.](https://github.com/MoscatelliMarco/stripe-mongoose-api)