Databases
MongoDB
The most popular document NoSQL database, storing data as JSON-like documents (BSON).
What it is
MongoDB stores data as documents in collections, not rows in tables. A document has a flexible structure, can hold nested objects and arrays, and the schema can change without migrations.
It has a rich query language and aggregation pipeline, indexes, multi-document transactions, replication (replica sets) and sharding for horizontal scaling. Atlas is the managed cloud version.
The SSPL license is not open source by the OSI definition: cloud providers cannot offer it as a service without conditions. Using it inside your own application has no restrictions.
Use it when
- Data with a changing structure: catalogs, content, profiles.
- A fast prototype where the schema still evolves.
- You need horizontal scaling with sharding.
Avoid it when
- Many relations between entities and complex joins: a relational database.
- Strict financial invariants across many tables.
Key concepts
- Document / Collection
- A BSON document and a group of documents.
- Aggregation pipeline
- A sequence of stages like
$match,$group,$lookup. - Replica set
- A group of nodes with one primary and replicas.
- Sharding
- Spreading a collection across nodes by a shard key.
Example
Interview questions
1. When to embed documents and when to reference?
Embed data that is read together and has bounded size. Use references for large, shared or frequently changing entities.
2. Does MongoDB support transactions?
Yes, multi-document ACID transactions exist since version 4.0. Still, the document model usually lets you rely on atomic operations on a single document.
3. What is a shard key and why does it matter?
It is the field data is distributed by across shards. A bad key causes load skew (a hot shard) and slow scatter-gather queries.
Comparisons with this technology
Related technologies
- PostgreSQLA powerful open source relational database with extensions, JSONB, full-text search and strong SQL support.
- Amazon DynamoDBA fully managed AWS key-value and document database with predictable latency and automatic scaling.
- Apache CassandraA distributed wide-column database with no single point of failure, built for massive write volumes.
- PrismaAn ORM for Node.js and TypeScript with a schema file, a generated type-safe client and migrations.
- ElasticsearchElasticsearch is a distributed search and analytics engine built on Lucene with a REST API and JSON documents.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).