Comparing Database Types to MongoDB
Tags: mongodb, javascript, node, database
Published: 2021-10-13 18:47:40

A **library**. We know this place has tons of books, novels, magazines, and other products in-store. If we think that this library is a *database*, we can think of their organization method (alphabetical, category, author, etc...) as a *database type*. **Database types** are patterns and structures used to organize data.

The beginnings of database types go back to the early-60s, and through the years these technologies experienced an amazing development. Some of these historic types have served as the base to advanced database technologies, however, new types have been developed to solve new issues and requirements in modern DBMS. In 1969, **relational databases** were introduced. This database type worked with tables, having primary keys and foreign keys that connect each table. Like this, tables can organize specific information and refer to other tables' information if needed.

To access and manipulate this DBMS, a querying language named **SQL** (Structured Query Language) was created. It connects tables through primary-foreign keys and allows to *filter data using constraints*. Like this, we can write complex queries that are very powerful and useful.

```SQL
/* to retrieve all date in a table: */
SELECT * FROM TABLE_NAME;

/* we can filter the data using constraints (WHERE).
If we have a PERSON table with attributes such as Name and Age we can filter the results depending on conditions */
SELECT Name FROM PERSON 
WHERE Age > 18;
```

But as the years pass, newer and better technologies are being developed to facilitate developers' jobs. **NoSQL databases** are modern database types that manage data differently from the standard relational pattern. Although the name might sound like is the opposite of SQL, it stands for "Not-only SQL" or "Non-SQL", meaning that *it may allow SQL-like querying*.

Some of them are **Key-value databases**, which have a key and a value (self-explanatory, right?). They can store simple data such as a JSON object, an image, or plain text. We access the data using the key name. 

**Document databases** follow the same semantic as key-value databases, but this time they don't have random pieces of data, instead, they store *entire documents*, often using formats like JSON or XML. Each document can have a different internal structure, and sometimes this type of database relates to both relational and key-value databases. **MongoDB** is an example of a document database. 

How does **MongoDB** compare to the relational model? As we mentioned above, the relational model stores different data in separate tables. Having dozens and dozens of tables can result in a massive amount of complexity to our application. This includes: 
- Being hard for people to use and understand.
- Difficult to add new features, having in mind all the tables related.
- Retrieving data from many tables involves code statements that might slow down our application.

Unlike the relational model, MongoDB stores data using a document, and here we can think about a real piece of paper with specific data. Documents are a simple way to structure your data, being easier for computers (and humans) to process and understand.

```javascript
// writing the same queries above, now using MongoDB
// .find() with a {} (empty document) inside , retrieves all the data within the collection
db.collection.find({});

// This query will retrieve all individuals over 18 years old
db.person.find({ age: {$gt: 18}});
```

This DBMS makes adding new data easy, not having to worry about breaking any other data. It also knows how to coordinate multiple servers to store data, so if one server fails we don't have to worry about our application being affected.