1. Introduction

MongoDB is a document-oriented NoSQL database. Unlike traditional relational databases with their tables and rows, MongoDB stores data in flexible documents similar to JSON.

What is NoSQL?

NoSQL does not mean "no SQL", but rather "Not Only SQL". It is the belief that your persistence layer is not necessarily the responsibility of a single system. You can use MySQL for certain parts of your application, Redis for others, and MongoDB for the rest.

Why MongoDB?

MongoDB should be seen as a direct alternative to relational databases. It is a generalist solution that can do what many other tools do. Some things it does better, others worse.

Main advantages:

  • Flexible schema: you don't need to define the structure of your tables in advance
  • JSON documents: you work with objects native to your programming language
  • Arrays as first-class citizens: you can store lists directly
  • Powerful queries: with advanced operators and aggregations
  • Horizontal scalability: through sharding
  • High availability: through replica sets

CAP theorem

The CAP theorem (also called Brewer's theorem) states that in a distributed system you can only guarantee 2 of these 3 properties simultaneously:

  • Consistency: all nodes see the same data at the same time
  • Availability: the system always responds, even if some node fails
  • Partition tolerance: the system works even when there are network failures between nodes

When you design a distributed database, you must choose which property you are willing to sacrifice:

Combination Description Example databases
CP Consistency + Partition tolerance. Sacrifices availability during network partitions MongoDB (default configuration), HBase, Redis (in cluster mode)
AP Availability + Partition tolerance. Sacrifices temporary consistency Cassandra, CouchDB, DynamoDB
CA Consistency + Availability. Does not tolerate network partitions (non-distributed systems) PostgreSQL, MySQL, SQL Server (single-node mode)

By default MongoDB prioritizes consistency over availability. When there is a network partition, it prefers not to respond rather than serve inconsistent data. But you can configure it to be more available if you need it.

ACID vs BASE

Traditional databases follow the ACID model:

  • Atomicity: transactions complete fully or are not performed at all
  • Consistency: data always meets the defined rules
  • Isolation: concurrent transactions do not affect each other
  • Durability: committed data is never lost

MongoDB has supported ACID transactions since version 4.0, but historically NoSQL databases have preferred the BASE model:

  • Basically Available: the system responds most of the time
  • Soft state: the state can change without external intervention due to replication
  • Eventual consistency: data will become consistent eventually

This means MongoDB can operate in both worlds, giving you the flexibility to choose according to your needs.

Key concepts

Before starting, you need to understand 6 concepts:

  1. Database: top-level container, as in relational systems
  2. Collection: equivalent to a table, but without a fixed schema
  3. Document: equivalent to a row, but in JSON format
  4. Field: equivalent to a column
  5. Index: improves query performance
  6. Cursor: pointer to the result of a query

The fundamental difference is that relational databases define columns at the table level, while MongoDB defines fields at the document level. Each document in a collection can have completely different fields.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.