Skip to article content
FrontendXLesson 17 / 24

MongoDB documents and the document model

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

intermediate · about 55 minutes · 2 exercises

  • Predict
  • Explain

What you will be able to do

#
  • Describe documents, collections and BSON types.
  • Choose between embedding and referencing.
  • Reason about single-document atomicity.

Overview

#

MongoDB stores JSON-like documents in collections. A document is a self-contained object with an _id, and related data can be embedded inside it or referenced by id. The document below uses mongosh shell notation (ObjectId and ISODate); in Node.js those BSON types come from the driver's ObjectId and a JavaScript Date.

Embed versus reference

Embed when the child is always read with the parent and is bounded in size, for example an address on a user. Reference when the child is large, unbounded, shared or updated independently, for example comments on a popular article.

snippet.js
{
  _id: ObjectId('...'),
  title: 'Indexes',
  author: { id: ObjectId('...'), name: 'Ada' },
  tags: ['database', 'sql'],
  createdAt: ISODate('2026-01-01T00:00:00Z')
}

Atomicity

Writes to a single document are atomic. A multi-document transaction exists, but it is more expensive, so a good document model keeps related invariants inside one document when it can.

Common pitfalls

  • Embedding an unbounded array that grows without limit, which eventually exceeds the document size limit.
  • Modeling a relational schema as many tiny collections and joining in application code.
  • Duplicating a value in many documents and letting it drift.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Explain

    For a social feed where a post can have millions of likes, should likes be embedded or referenced? Explain.

    starter.text
    post = { _id, body, likes: [...] }
    

    Reveal the reference answer
    reference.text
    Reference them. An embedded likes array is unbounded and will exceed the document size limit, and updating one like rewrites the whole document. Store likes in a separate collection keyed by post id, or keep a counter on the post and the detailed records elsewhere.

    Authored expected output: Recommends referencing because the array is unbounded and updates would be expensive.

    Embedding suits bounded, parent-scoped data. Unbounded collections belong in their own collection.

  2. Exercise 2Predict

    You update two fields of one document in a single updateOne. If the process crashes mid-write, what state is the document in?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.txt
    db.posts.updateOne({ _id }, { $set: { title: 'A', body: 'B' } })
    Choose the best answer

Common misconceptions

#
  • MongoDB is schemaless so anything goes: the application still depends on a document shape, and validation belongs somewhere.
  • Embedding is always faster: an unbounded embedded array becomes a performance and size problem.

Follow-up practice

#
  • backend-databases-mongoose

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.