Collections and Databases
What You'll Learn
You'll learn how document stores organize individual documents into collections and group collections into databases—the hierarchical structure that makes document stores manageable at scale.
The Organizational Hierarchy
In a document store, collections are groups of related documents, similar to how tables group rows in a relational database. For example, all user profile documents might live in a users collection, while product documents live in a products collection.
Databases are the top-level organizational unit. They contain multiple collections and provide a namespace boundary. You might have a production database, a staging database, or separate databases for different applications or tenants.
The Library Analogy
Think of a document store like a library system:
- Database = The entire library building
- Collection = A specific section (Fiction, Non-Fiction, Reference)
- Document = An individual book
Just as all mystery novels are shelved together in the Fiction section, all user documents are stored together in the users collection within your application's database.
Why This Structure Matters
This hierarchy provides:
- Logical separation: Different document types don't mix
- Access control: You can grant permissions at the database or collection level
- Query scope: Queries run against a specific collection, not the entire database
- Organizational clarity: Related data stays together
Unlike relational databases where foreign keys enforce relationships between tables, document stores treat collections more independently. Each collection is its own namespace—documents in different collections don't inherently relate to each other unless your application logic creates those connections.
Key Takeaway: Document stores organize documents into collections (like tables) and group collections into databases, creating a clear hierarchy that simplifies management, security, and querying while maintaining the flexibility of schema-free documents.