When learning MongoDB, one of the most important concepts to understand is the collection. It sits right at the heart of how data is organized and accessed in MongoDB.
In this blog post, we’ll cover what a collection is, how it works, how it differs from tables, and why it’s so powerful in real-world applications.
What Is a Collection in MongoDB?
A collection in MongoDB is a group of documents stored within a database.
Think of the MongoDB data structure like this:
Database
└── Collection
└── Document
└── Fields
In simple terms:
A database contains collections
A collection contains documents
A document contains key–value pairs
MongoDB collections are part of the MongoDB document-oriented data model.
Collection vs Table
Table (Relational Databases – SQL)
A table is a structured storage unit used in relational databases like MySQL, PostgreSQL, or SQL Server.
Data is stored in rows and columns
Every row must follow the same predefined schema
Column names and data types are fixed
Relationships are maintained using primary keys and foreign keys
Best suited for structured data where consistency is critical
Example:
A Users table where every row has the same columns like Id, Name, Email, Age.
Collection (NoSQL Databases – MongoDB)
A collection is a container used in NoSQL databases like MongoDB.
Data is stored as documents (BSON/JSON format)
Documents inside a collection do not require the same structure
Schema is flexible (schema-less)
No joins by default; data is often embedded
Ideal for semi-structured or evolving data
Example:
A users collection where one document has email, another has phone, and another has both
Key Difference
SQL tables enforce a strict schema, while MongoDB collections are schema-flexible.
Key Characteristics of MongoDB Collections
Schema-less (Flexible Schema)
Documents in the same collection do not need to have the same structure.
{ name: "Raj", age: 25 }
{ name: "Amit", skills: ["MongoDB", "Node.js"] }
Both documents can exist in the same collection.
Dynamic Creation
Collections are created automatically when you insert the first document.
db.users.insertOne({ name: "Raj" })
No need to explicitly create a collection.
Stores BSON Documents
Collections store documents in BSON format, which supports rich datatypes like:
ObjectId
Date
Array
Embedded documents
Supports Indexing
Indexes can be created on collections to improve query performance.
db.users.createIndex({ email: 1 })
High Scalability
Collections can be:
This makes MongoDB ideal for large-scale applications.
How to Create a Collection
Automatically (Recommended)
use myDatabase
db.products.insertOne({ name: "Laptop", price: 50000 })
Collection products is created automatically.
Manually
db.createCollection("employees")
Used when you want to apply options like validation or capped collections.
Types of Collections in MongoDB
Normal Collection
Standard collection used for most data.
Capped Collection
Fixed-size collection that maintains insertion order.
db.createCollection("logs", { capped: true, size: 1024 })
Useful for logs and audit data.
Time-Series Collection
Optimized for time-based data such as IoT or metrics.
db.createCollection("sensorData", {
timeseries: {
timeField: "timestamp",
metaField: "device"
}
})
Common Collection Operations
| Operation | Command |
|---|
| Show collections | show collections |
| Insert document | insertOne() |
| Find documents | find() |
| Update document | updateOne() |
| Delete document | deleteOne() |
| Drop collection | db.collection.drop() |
Where Are Collections Stored Physically?
Collections are stored:
You don’t directly interact with files—MongoDB handles storage internally.
Common Interview Questions
What is a collection in MongoDB?
A collection in MongoDB is a group of documents stored within a database.
It is similar to a table in SQL
Documents inside a collection are stored in BSON format
Collections do not require a fixed schema
Example:
A users collection can store user-related documents.
{ "name": "Raj", "age": 25 }
{ "name": "Amit", "email": "[email protected]" }
Can documents in a collection have different schemas?
Yes
MongoDB collections are schema-less.
Each document can have different fields
Fields can have different data types
This gives flexibility for evolving applications
Example (same collection, different structure):
{ "name": "Raj", "age": 25 }
{ "name": "Anita", "skills": ["Java", "MongoDB"] }
Difference between a collection and a table?
| Feature | Collection (MongoDB) | Table (SQL) |
|---|
| Schema | Flexible / Schema-less | Fixed schema |
| Data format | BSON (JSON-like) | Rows & Columns |
| Structure | Documents | Records |
| Alter schema | Easy | Complex |
| Scalability | Horizontal | Mostly Vertical |
Short interview answer:
A collection is flexible and schema-less, while a table has a fixed structure.
What is a capped collection?
A capped collection is a fixed-size collection that works like a circular queue.
Use cases:
Logs
Event streaming
Real-time data
Example:
db.createCollection("logs", { capped: true, size: 1024 })
How does MongoDB store collections internally?
Internally, MongoDB:
Stores data in BSON format
Uses a storage engine (default: WiredTiger)
Each collection maps to data files on disk
Indexes are stored separately for fast access
Internals include:
Collection metadata
Indexes
Data pages
Journaling for recovery
Can a collection exist without documents?
Yes
Example:
db.createCollection("students")
This creates a collection with 0 documents.
Conclusion
A collection is the backbone of MongoDB data organization. Its flexible schema, scalability, and performance advantages make it a powerful alternative to traditional SQL tables. Understanding collections helps you design efficient MongoDB schemas and build scalable real-world applications.
Thank you for taking the time to read this post.
I hope this guide helped you understand what collections are in MongoDB, How does MongoDB store collections , schema-less design plays a crucial role in building scalable and modern NoSQL applications.