Section 1 · Foundations

Documents, collections, and dot notation

MongoDB stores documents — flexible JSON-like objects — grouped into collections. There are no fixed columns; a document can contain arrays and nested objects. That flexibility is exactly why aggregation pipelines are so powerful later.

🍃A document is a JSON object with an _id

Run this to see one product document. Notice it holds a whole array (tags) inside a single record — no separate "tags table" needed:

One document
findOne returns a single document
Every document has an _id It's the primary key — unique within the collection. In real MongoDB it's usually an ObjectId (a 24-char hex value); here we use simple numbers to keep things readable. If you insert without an _id, one is generated for you.

📚Collections = tables, documents = rows (roughly)

See a whole collection at once with find() and no filter:

Browse a collection
try: db.products.find() · db.orders.find()
SQL wordMongoDB word
tablecollection
rowdocument
columnfield
primary key_id
SELECT … WHEREfind({...})
GROUP BY + joinsthe aggregate([...]) pipeline

🔎Reaching inside: dot notation

Documents nest. An order holds an items array, and each item is itself a little document with product, qty, price. To reach a nested field you use a dotted path in quotes, like 'items.product'. Run this and look at the shape of an order:

A document with a nested array
see items: [ { product, qty, price }, ... ]

Now query by a value inside that array. Because items is an array of objects, { 'items.product': 'Latte' } matches any order containing a Latte item:

Query into an array with a dotted path
the path must be in quotes because of the dot
Quote dotted paths items.product without quotes is invalid JavaScript. Always write nested keys as strings: { "items.product": "Latte" }. Single-word keys like price don't need quotes.

🧪Free play

Your shell
Ctrl+Enter runs · try findOne, or a different collection
🧠

Checkpoint — documents

1. In MongoDB, what is a "collection"?
A collection holds many documents, the way a SQL table holds many rows. But documents don't need identical shapes.
2. How do you query a field inside a nested array of objects?
Dot notation in a quoted string. It matches if any array element has that value.
3. What is _id?
Every document has a unique _id. Omit it on insert and MongoDB generates one.
🔓 You're reading a free chapter of MongoDB — the first two are open.
Unlock the rest of this course with a one-time payment.
Unlock this course →
🔓 See course prices