Store each record as a flexible JSON-like document (MongoDB, DynamoDB).
A document database stores each record as one document: a flexible, JSON-like object that can hold nested fields and lists. Documents live in collections.
In a relational database, one order might be spread across three or four tables. In a document database, you can store the whole order as one JSON-like document: the customer, the address and the list of items, all inside it. That is called embedding.
Each document has an _id (a unique key). You can query by other fields too, and add indexes on them. Because the shape is not forced, you can add a new field to new documents without changing the old ones. This is often called being schema-flexible. (You still have a schema in your code. The database simply does not force it by default, although most let you add validation rules.)
| Choice | What it means | Use when |
|---|---|---|
| Embed | Put the related data inside the document. | Read together, small, owned by this document (an order and its lines). |
| Reference | Store the id of another document and fetch it separately. | Shared by many, changes on its own, or grows without limit (users, products, comments). |
| Copy a few fields (extended reference) | Embed only the fields you show often, like the product name, and keep the id too. | You want fast reads and can accept that the copy may be slightly old. |
| Bucket | Group many small items into one document, for example one document per hour of readings. | Lots of tiny records, such as time-based data. |
| Product | Notes (general idea, varies by version) |
|---|---|
| MongoDB | BSON documents, rich queries, aggregation pipeline, multi-document transactions, 16 MB document limit. |
| Amazon DynamoDB | Items found by key. Fast and managed. Items are limited to 400 KB. Query by key plus a few indexes. |
| Couchbase / CouchDB | JSON documents with sync features. Couchbase also offers a SQL-like language (N1QL). |
| Firestore | Managed, real-time updates, built for mobile and web apps. |
{
_id: 9001,
customer: { name: "Asha", city: "Pune" },
items: [
{ sku: "P1", name: "Product 1", qty: 1 }
{ sku: "P2", name: "Product 2", qty: 1 }
{ sku: "P3", name: "Product 3", qty: 1 }
... 2 more
]
}A simplified model with fixed sizes (about 400 bytes per item or comment) and counts, no timers or random numbers. Rule of thumb: embed what is read together and stays small. Reference what is shared, changes on its own, or can grow without bound.
{
"_id": 9001,
"customer": { "name": "Asha", "city": "Pune" },
"items": [
{ "sku": "P1", "name": "Book", "qty": 1, "price": 12 },
{ "sku": "P2", "name": "Pen", "qty": 3, "price": 3 }
],
"status": "paid"
}
db.orders.createIndex({ "customer.name": 1 })
db.orders.find({ "customer.name": "Asha", status: "paid" })| Term | Meaning in easy words |
|---|---|
| Document | One record stored as a JSON-like object with nested fields |
| Collection | A group of documents, like a table but without a forced shape |
| _id | The unique key of a document |
| Embedding | Storing related data inside the same document |
| Reference | Storing the id of another document instead of copying it |
| Schema-flexible | Documents in a collection may have different fields |
| BSON | The binary form of JSON that MongoDB stores |
| Secondary index | An index on a field other than the key, to speed up queries |
| Shard key | The field used to decide which machine stores a document |
| Aggregation pipeline | A series of stages (filter, group, sort) that process documents |
| $lookup | A MongoDB stage that joins documents from another collection |
| Choice | We gain | We lose |
|---|---|---|
| Embed | One quick read, atomic write | Copies, big documents, hard to share |
| Reference | One source of truth, small documents | More reads and app code |
| Flexible schema | Faster change | Less protection from bad data |
| More indexes | Faster queries | Slower writes, more memory |
| Multi-document transactions | Cross-document correctness | More overhead and less scale |