Lesson 9/100

Tutorials MongoDB Tutorial

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 & CRUDQueries & SchemaAggregation & ScaleAtlas & 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.

Code
Result

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

  1. Insert the order document (or let MongoDB auto-create _id).
  2. Run findOne and expand items in Compass.
  3. Add a field couponCode with updateOne (next lessons).
  4. Embed shipping: { city: "Bengaluru", pincode: "560001" }.
  5. 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.

Junior Detailed
Explain SQL queries in the context of MongoDB.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MongoDB?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MongoDB application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MongoDB project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MongoDB Tutorial
Course syllabus

MongoDB Tutorial

MongoDB — Foundations
MongoDB — CRUD Operations
MongoDB — Query Operators
MongoDB — Schema Design
MongoDB — Indexing & Performance
MongoDB — Aggregation Pipelines
MongoDB — Replication & Sharding
MongoDB — Atlas & Security
MongoDB — Modern Features
MongoDB — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details