MongoDB β Flexible Document Database
Before you start: basic familiarity with what a database is for (storing and retrieving data reliably) is assumed, but no prior MongoDB or NoSQL experience is needed. If you've done the SQL Mastery page, the comparisons below will click faster, but it isn't required β this page explains the document model from scratch either way.
A document database stores each record as a single self-contained unit (a document β essentially one JSON object) rather than splitting it across multiple linked tables. MongoDB is the most widely used document database: it stores data as JSON-like documents instead of rows and tables, and β unlike a traditional table, where every row must have the same columns β each document can have a different structure. That flexibility is what makes it a good fit for evolving schemas, nested data, and applications that don't fit neatly into the relational (table-based) model.
Document vs Row
When to Use MongoDB
Good fit:
Not a good fit:
A note on financial transactions: it's a common but outdated claim that MongoDB "can't do ACID" and is therefore unsuitable for financial data β MongoDB has supported multi-document ACID transactions since version 4.0 (2018, single replica set) and 4.2 (sharded clusters). Financial systems still often lean toward relational databases, but the real reasons are usually schema rigidity/constraints being a good fit for regulated data, mature reporting/JOIN tooling, and organizational familiarity β not a technical ACID limitation that no longer exists. MongoDB's own material even recommends single-document atomicity (via schema design) over multi-document transactions where possible, due to the performance cost of the latter β but "not possible at all" is simply inaccurate.
The Aggregation Pipeline
MongoDB's most powerful feature for analysis is the aggregation pipeline: instead of one query trying to do everything at once, you chain together simple stages, each one taking the previous stage's output and transforming it further β the same idea as piping commands together in a shell, but for documents. A typical pipeline for "revenue by city, from completed orders" looks like this:
Each stage is independently simple β the power comes from chaining them, the same way a handful of simple shell commands piped together can do something none of them could alone.
Quick Start
More Aggregation Stages
Computed Pattern β Pre-Calculate Aggregates
Python with PyMongo
Interview Questions
What is the CAP theorem and where does MongoDB sit?
CAP theorem states a distributed system can guarantee at most two of three: Consistency, Availability, Partition tolerance. MongoDB prioritizes Consistency and Partition tolerance (CP). With default write concern w:1, a write is acknowledged when the primary confirms it β replicas may briefly lag. With w:majority, writes require acknowledgment from most replica set members, ensuring stronger consistency at the cost of slightly higher latency. MongoDB sacrifices some availability (during network partitions, the minority partition becomes read-only) to maintain consistency.
Explain the aggregation pipeline vs. MapReduce.
The aggregation pipeline processes documents through sequential stages β each stage transforms the data and passes it to the next. It's native, highly optimized, and runs entirely in the database engine. MapReduce in MongoDB uses JavaScript functions running in a separate interpreter β much slower, harder to debug, and effectively legacy at this point in favor of the aggregation pipeline. Always use the aggregation pipeline β the only historical reason MapReduce existed was for complex logic that pipeline stages couldn't express, and the aggregation pipeline's $function and $accumulator stages now cover those cases too.

