Reference · Cheat sheet
Lesson 0001 distilled — the document is the unit of storage, atomicity, and schema design, and everything that follows. Built to print.
A document (BSON — binary JSON, extra types) is the unit of storage, atomicity, and schema design — all three.
It's a tree: fields can be nested documents and arrays. Documents live in a collection (≈ table, but schema-free).
_id = primary key, unique per collection; auto-set to an ObjectId if omitted.
16 MB max per document (guards RAM + bandwidth). Bigger → GridFS or reference out.
Data accessed together is stored together. Model to your access pattern.
Embed by default → read one entity = one document lookup (no join). Reference (store an _id) when data is large, unbounded, or accessed independently.
Single-document write = atomic, even across many fields/nested arrays — no transaction needed.
Multi-document atomic = needs a distributed transaction (costs more; often avoidable by embedding).
| Aspect | Postgres/InnoDB | MongoDB |
|---|---|---|
| Unit | row | document |
| Schema | fixed | flexible |
| Relate | normalize + join | embed / reference |
| Read entity | join tables | one document |
| Atomic on | transaction | document |
db.posts.insertOne({…embedded…}) → _id auto-created.
One updateOne with $push+$set changes an array and a nested field — atomic, no txn.