MongoDB
Also known as: Mongo, MongoDB Atlas, BSON, document database
MongoDB is a NoSQL database where data is stored as documents (JSON-like objects) rather than rows in tables.
It's flexible (documents in the same collection can have different fields) making it popular for applications where data structures evolve quickly or vary per record.
MongoDB, explained at three levels
At home
A regular database stores data in rows and columns, like a spreadsheet. MongoDB stores data as documents (like JSON objects) so a product can have any fields you want without redesigning the database. It's more flexible but less structured.
At work
Teams choose MongoDB when their data doesn't fit neatly into tables: user profiles with varied fields, product catalogues with different attributes per category, or event logs with evolving schemas. Documents map naturally to the objects that application code works with.
At home
MongoDB stores documents (BSON, a binary JSON format) in collections (analogous to tables). Queries use a JSON-like query language: `{age: {$gte: 18}, city: 'Toronto'}`. Indexes improve query performance. The aggregation pipeline allows complex transformations: group by, join (lookup), project, sort, and window functions.
At work
MongoDB Atlas (managed cloud service) provides auto-scaling, global clusters, full-text search (Atlas Search, powered by Lucene), vector search (Atlas Vector Search), and Atlas Data Federation for querying data in S3. Change Streams allow real-time event notifications on data changes: useful for event-driven architectures.
At home
MongoDB's schema flexibility requires discipline: without enforced schemas, inconsistent document structures cause application bugs. Schema validation (using JSON Schema) can be applied at the collection level to catch invalid documents on insert. The document model encourages denormalization (embedding related data) to avoid joins, which improves read performance but requires managing data duplication.
At work
MongoDB's replication uses a replica set (primary + secondaries) with election-based failover. Sharding partitions data by shard key across multiple shards for horizontal scaling. Choosing the right shard key is critical: a poor choice causes hot spots. Multi-document ACID transactions work across collections and sessions but carry performance overhead, so they're used selectively.
Real-world examples
At home
Fatima built a recipe app using MongoDB because each recipe has a different structure: some have nutritional info, some have step images, some have video links. MongoDB lets her store whatever fields each recipe has without empty columns.
At work
Sophie's team uses MongoDB for their product catalogue (10 million products, each with different attributes depending on category) and PostgreSQL for orders and payments. The mixed approach uses each database where it fits best.
Common confusions
- MongoDB is better than SQL databases — MongoDB and relational databases (PostgreSQL, MySQL) have different strengths. MongoDB fits flexible, document-shaped data with fast iteration. SQL databases are better for complex relationships, reporting, and strong consistency requirements. Neither is universally better.
- MongoDB doesn't support transactions — MongoDB has supported multi-document ACID transactions since version 4.0 (2018). Earlier versions only had single-document atomicity, which gave MongoDB its reputation for lacking transactions, but this is no longer accurate.
For analysts
BAs can connect BI tools such as Tableau and Power BI to MongoDB Atlas through its SQL interface, so they can query MongoDB with SQL. Alternatively, MongoDB's aggregation pipeline can produce summary reports directly from the database.
History & origin
MongoDB was created in 2007 by Dwight Merriman, Eliot Horowitz, and Kevin Ryan, two of whom had co-founded DoubleClick, frustrated by the rigidity of relational databases for large-scale web applications. The name comes from 'humongous': reflecting its target for large datasets. MongoDB went public in 2017. It popularized the document-oriented NoSQL model and the term 'schemaless' database. Despite early criticisms about default settings allowing data loss, MongoDB matured significantly: adding multi-document ACID transactions in version 4.0 (2018).