MongoDB
MongoDB is an open-source document database. Instead of rows in tables, it stores JSON-like documents in collections, and each document can contain nested objects and arrays. It has a rich query language, secondary indexes, an aggregation framework, built-in replication and built-in sharding, and it's available as a managed service (MongoDB Atlas) on all the major clouds.
The properties that matter:
- Document model: a document holds an entity and its nested parts together, like an order with its line items and shipping address. Data that's read together can be stored together, so most reads are one lookup with no joins.
- Flexible schema: documents in the same collection don't have to share a shape. You can add fields without a migration and enforce structure where you want it with schema validation.
- Rich queries and indexes: unlike pure key-value stores, you can query and index on any field, including fields inside nested documents and arrays.
- Replica sets and sharding built in: high availability through automatic failover, and horizontal scaling by splitting collections across shards by a shard key.
- Atomic per document: any single-document write is atomic. Multi-document ACID transactions exist, but they're the exception, not the main tool.
In interviews, MongoDB fits data that's naturally hierarchical and varies in shape: product catalogs, content management, user profiles with optional fields, game state, event payloads. Interviewers will probe on how you model the data (embed or reference?), how you choose a shard key, and what consistency you get from replicas. That's what we'll cover.
The Document Model
A document is stored as BSON, a binary JSON format with extra types (dates, 64-bit integers, decimals, binary data, ObjectIds). Documents live in collections, which live in databases. Every document has a unique _id, by default an ObjectId: a 12-byte value made of a 4-byte timestamp, a 5-byte random value and a 3-byte counter, so IDs are roughly time-ordered and can be generated by any client without coordination.
{
"_id": { "$oid": "6749a1f2c3e4b5a6d7e8f901" },
"product_id": "TSHIRT-RED",
"title": "Classic Tee",
"price": { "amount": 1999, "currency": "USD" },
"attributes": { "color": "red", "size": "M", "material": "cotton" },
"categories": ["apparel", "tops"],
"variants": [
{ "sku": "TSHIRT-RED-S", "stock": 12 },
{ "sku": "TSHIRT-RED-M", "stock": 0 }
],
"updated_at": { "$date": "2026-11-29T10:00:00Z" }
}
A document can be up to 16 MB. That's large for structured data, but it's a hard limit, and growing documents toward it is a design smell.
The flexible schema is a convenience, not a license to have no schema. Your application still assumes fields exist and have certain types. Schema validation ($jsonSchema rules on a collection) lets the database reject documents that don't match, which is a good idea for anything important.
Embed or Reference?
The central modeling question in MongoDB is whether related data lives inside a document or in a separate collection linked by ID.
| Embed when... | Reference when... |
|---|---|
| The data is read together almost every time | The related data is often read on its own |
| It's "owned" by the parent (order line items, an address) | It's shared by many parents (a product referenced by many orders) |
| The number of children is small and bounded | The number of children is large or unbounded |
| You want atomic updates across parent and children | Children change frequently and independently |
A blog post with its last few comments embedded reads in one query. A post with every comment ever embedded grows without limit, hits the 16 MB cap and rewrites a huge document on every new comment. So you'd store comments in their own collection, keyed by post_id, and perhaps embed only the latest three for the post page (the subset pattern).
A few other patterns come up often:
- Extended reference: copy the few fields you need from a referenced document (the customer's name and email into the order) to avoid a second lookup, accepting that copies can go stale.
- Bucket: group many small items into one document per time window, like one document per sensor per hour holding an array of readings. Fewer, larger documents mean fewer index entries and faster range reads.
- Computed: maintain precomputed values (a
review_countandaverage_ratingon a product) updated on write, instead of aggregating on every read.
In an interview, show both the document shape and the reasoning: "Orders embed their line items and shipping address, because they're always read with the order and never change after checkout. Products are referenced by SKU, with the title and price at purchase time copied into the line item." Interviewers want to see that you embedded for a reason, not by default.