MongoDB organizes data in a strict top-down hierarchy. Understanding this model determines how you design your schema, run queries, and scope operations like drops and backups.
Containment Model
Server
└── Database (logical grouping of collections)
└── Collection (grouping of related documents)
└── Document (a single BSON record)
└── Fields (key-value pairs within the document)
A single MongoDB server (or replica set / sharded cluster) can host many databases. Each database holds many collections, each collection holds many documents, and each document holds many fields.
Reserved Databases
MongoDB ships with three built-in databases you should never accidentally drop or repurpose:
| Name | Purpose | Notes |
|---|---|---|
admin |
Authentication, authorization, and server-wide commands | Root user lives here; db.adminCommand() always targets this DB |
local |
Stores the oplog and replication state | Never replicated to secondaries — each node maintains its own copy |
config |
Sharding metadata — chunk ranges, shard mappings, balancer state | Managed by mongos; do not write to it manually |
Lazy Creation — The Core Concept
Unlike relational databases, MongoDB does not physically create a database or collection until you write actual data into it. Switching to a database with use myDB is purely a context change in the shell — no disk allocation, no namespace reservation, nothing persists until the first write.
CREATE DATABASE or CREATE TABLE before inserting. Just insert and the structure materializes automatically on disk.