Catalog My learning Blog
MongoDB Tutorial: Schema Design, Indexing and Sharding · Phase 1 · Lesson 1 · 3 min read

What Is MongoDB? Document Databases Explained

On this page
  1. One order in five tables vs one document
  2. MongoDB documents, collections and databases
  3. Is MongoDB schemaless? Not quite
  4. When to use MongoDB vs a relational database
  5. Check yourself

One order in five tables vs one document

In a relational database, an order with its lines, shipping address and payment details spans several tables: orders, order_lines, addresses, payments. Showing one order means joining them. Saving one means inserting into each, inside a transaction.

In MongoDB, the same order is one document:

JSON
{
  "_id": 991,
  "customerId": 42,
  "status": "paid",
  "placedAt": { "$date": "2026-10-10T09:14:00Z" },
  "shipTo": { "street": "Main St 1", "city": "Berlin", "country": "DE" },
  "lines": [
    { "sku": "LAMP-1", "name": "Desk lamp", "qty": 1, "price": { "$numberDecimal": "39.00" } },
    { "sku": "BULB-2", "name": "LED bulb", "qty": 2, "price": { "$numberDecimal": "4.50" } }
  ],
  "total": { "$numberDecimal": "48.00" }
}

Reading the order is one lookup. Writing it is one operation, and a write to a single document is atomic, even with nested arrays, without a multi-table transaction.

A relational order spread across joined tables, compared with one MongoDB document that embeds the address and order lines.
A relational order spread across joined tables, compared with one MongoDB document that embeds the address and order lines.

MongoDB documents, collections and databases

  • A document is a set of fields, which can hold values, nested documents and arrays. It's stored as BSON (Day 3).
  • A collection groups documents, roughly like a table.
  • A database groups collections.

Every document has a unique _id. If you don't provide one, MongoDB generates an ObjectId.

Is MongoDB schemaless? Not quite

Documents in one collection don't have to share a shape, and you don't run ALTER TABLE to add a field. That makes evolving data easier. But your application still expects a shape, and data without enforced rules drifts: one service writes email, another Email, a third stores dates as strings. So MongoDB has schema validation (Day 5), and good teams use it. The schema is flexible, not absent.

When to use MongoDB vs a relational database

MongoDB fits well when the main access pattern is "get or update one thing and its parts":

  • product catalogs where each category has different attributes,
  • user profiles and settings,
  • content management, orders, carts,
  • event and IoT data with varied payloads.

It fits less well when the workload is mostly relational: ad-hoc reports joining many independent entities, or many-to-many relationships queried from every direction. MongoDB can join ($lookup, Day 15) and supports multi-document transactions (Day 18), but if every query needs them, a relational database is usually simpler.

Relational (PostgreSQL, SQL Server)Document (MongoDB)
Unit of dataRows across normalized tablesDocuments with nested data
SchemaEnforced by the tableFlexible, optional validation
Reading an aggregateJoinsUsually one document
Atomic unitTransaction across rowsOne document (plus transactions when needed)
Ad-hoc cross-entity queriesExcellentPossible, often less natural
Scaling out writesSharding is an add-on or separate productBuilt-in sharding (Day 22)

PostgreSQL's jsonb covers some document use cases, and MongoDB covers some relational ones. The deciding question is the shape of your most common reads and writes.

Check yourself

The lines are embedded in the order document, and MongoDB writes to a single document atomically, so the order and all its lines are saved together or not at all. In a relational design the same save touches several tables and needs a transaction to get that guarantee.

No. The application still expects a shape; MongoDB just doesn't require it up front, and you can enforce one with schema validation.

When most queries join many independent entities, such as ad-hoc reporting across customers, products and orders, or when strong constraints between tables, like foreign keys, matter more than flexible documents. MongoDB fits best when data is read and written as self-contained aggregates.

A collection groups documents much as a table groups rows, but documents in a collection don't have to share the same fields, and each can contain nested objects and arrays. A database holds collections, as a schema holds tables.

How the application reads and writes the data. Data that's read together should usually be stored together in one document, while data that grows without bound or is accessed independently belongs in its own collection.