Stop 05 · MongoDB FundamentalsTopics · 2/9

Documents and Collections

Part of the MERN Stack Roadmap

In short

In a document database, you store data the way your app reads it. Get the document shape right and most pages need just one query.

MongoDB stores records as documents (JSON-like objects stored as BSON) inside collections. Each document gets a unique _id, and documents in one collection can have different fields.

The blob packs books, a lunchbox and a water bottle into one school backpack.

Think of it like this

Packing the school bag

You pack everything for the day into one bag: books, tiffin, bottle, pencil box. Grab it and go, with no trips back to the cupboard. A document works the same way: everything one screen needs, packed together and fetched in a single read.

Watch it happen

One order, one document

order #1042
user: Riya
items: [ ]
dosa x 2
coffee x 1
address: {…}

1/5One document for one order. Everything the order page shows is inside it.

The big ideas

What you need to know

Nested objects and arrays

Fields can hold objects and lists, many levels deep. Your stored data can look like your API response.

Embed what you read together

If you always show items with the order, keep them inside the order document.

Reference what grows forever

Comments on a viral post can grow without limit. Store them separately and point to the post's _id.

16 MB per document

One MongoDB document can't exceed 16 MB. Arrays that grow without limit are the usual way people hit it.

See it in code

Walk through the code

js
1// Embed: items are always shown with the order2db.orders.insertOne({3  user: { id: 7, name: "Riya" },4  items: [{ dish: "Masala dosa", qty: 2 }],5  address: { line1: "Hostel B, Room 12", city: "Pune" },6});7 8// Reference: reviews can grow without limit9db.reviews.insertOne({ dishId: 3, userId: 7, stars: 5, text: "Crispy!" });10db.reviews.find({ dishId: 3 }).sort({ _id: -1 }).limit(20);

1/3One order document holds the user snapshot, the items and the address together.

These run in mongosh. Rule of thumb: embed what you read together, reference what grows without limit.

Save yourself the pain

Common mistakes

Oops

Embedding every comment inside the post document.

Do this instead

Arrays that grow forever make documents huge and slow to update. Give them their own collection.

Oops

Splitting everything into many collections and joining in code, SQL-style.

Do this instead

That fights the document model. Shape documents around your screens so one read is enough.

Your mission

Shape a food-app order

~20 min

Quick check

A blog post can get thousands of comments. Where should the comments live?

Go deeper

  1. 1Write a sample product document with nested fields and an array of tags.
  2. 2Learn what an ObjectId is and what its timestamp part tells you.
  3. 3Name collections in plural lowercase, such as users and orders.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge