Documents
Documents: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 9 of 100
Documents
Foundations & CRUD → Queries & Schema → Aggregation & Scale → Atlas & Projects
Foundations & CRUD · 1 — Documents · ~6 min · MongoDB — Foundations
What is this?
A document is one BSON object with field–value pairs. Every document has _id (unique in its collection). Fields can be strings, numbers, arrays, nested objects, dates, and more.
Why should you care?
One order document can embed line items. That matches how a checkout API already thinks — one payload, one stored record.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.orders.insertOne({
_id: ObjectId("64f000000000000000000001"),
userId: ObjectId("64f0000000000000000000aa"),
items: [
{ sku: "KB-01", qty: 1, price: 4499 },
{ sku: "MS-02", qty: 2, price: 999 }
],
status: "placed",
placedAt: new Date()
})
db.orders.findOne({ status: "placed" })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- The order has a fixed _id, a reference-style userId, an items array of subdocuments, a status string, and a Date.
- findOne returns the first matching order.
- Nested items live inside the same document.
Practice next
- Insert the order document (or let MongoDB auto-create _id).
- Run findOne and expand items in Compass.
- Add a field couponCode with updateOne (next lessons).
- Embed shipping: { city: "Bengaluru", pincode: "560001" }.
- Insert without _id and inspect the auto ObjectId.
Remember
Documents are the unit of data in MongoDB. _id uniquely identifies each document in a collection. Nest related data when it is read together.
Swiggy-style order snapshot
When you place food, the order document stores restaurant name and item prices at order time even if the menu changes later.
Outcome: Historical orders stay accurate for support and invoices.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!