Course contentsShow
← System Design
Lesson 389 of 1,91911. NoSQL: Document StoresFree lesson

MongoDB Document Model and Collections

How MongoDB stores data as flexible JSON-like documents in collections instead of rigid tables and rows.

MongoDB Document Model and Collections

What you'll learn: How MongoDB organizes data into flexible JSON-like documents grouped within collections instead of using rigid table schemas.

Documents: The Building Blocks

MongoDB stores data as documents — structured records that look like JSON (technically BSON: Binary JSON). Each document is a self-contained unit of data with fields and values, similar to a JavaScript object or Python dictionary. Unlike relational database rows that must conform to a strict schema, MongoDB documents within the same collection can have different structures.

Think of a document like a filing folder: one folder might contain a resume, reference letters, and a photo, while another folder for a different person might only have a resume and a certificate. Both are valid "person" folders, but they don't need identical contents.

// Two valid documents in the same collection
{ "_id": 1, "name": "Alice", "email": "alice@example.com", "age": 28 }
{ "_id": 2, "name": "Bob", "email": "bob@example.com", "skills": ["Python", "Go"] }

Collections: Logical Groupings

A collection is a group of related documents, conceptually similar to a table in relational databases, but without enforcing a fixed schema. You might have a users collection, an orders collection, and a products collection.

The key difference from tables: MongoDB doesn't require every document in a collection to have the same fields. This schema flexibility lets your data model evolve naturally as requirements change.

Why This Matters for System Design

This model excels when your entities have varying attributes or when you need to iterate quickly without database migrations. MongoDB automatically creates collections when you insert the first document, and adding new fields requires no schema alterations.

Key Takeaway: MongoDB uses flexible, JSON-like documents organized into collections, allowing each document to have its own structure without enforcing rigid schemas across the collection.