What is a Collection in MongoDB?
Learn what a MongoDB collection is, how it compares to a SQL table, and how creation, indexing, and schema validation work per collection in practice.
Expected Interview Answer
A collection is a named grouping of MongoDB documents within a database, roughly analogous to a table in a relational database but without a required fixed schema across its documents.
Collections are created implicitly the first time you insert a document into them, or explicitly with options like validation rules or capped size limits. All documents in a collection typically represent the same kind of entity, such as `users` or `orders`, even though MongoDB does not force them to share identical fields. Indexes, including the default index on `_id`, are defined per collection to speed up queries. A database can contain many collections, and collections can optionally enforce schema validation rules to catch malformed documents at write time.
- Groups documents representing the same kind of entity
- Created implicitly on first insert, or explicitly with options
- Supports per-collection indexes for fast queries
- Optional schema validation for write-time consistency
- Analogous to a table, without a rigid shared schema
AI Mentor Explanation
A collection is like a team's full squad list, gathering every player's card under one roster even though a bowler's card and a batter's card record different details. The roster is named and managed as a unit, the way a MongoDB collection like 'players' groups documents that all represent the same kind of entity.
Step-by-Step Explanation
Step 1
Named grouping
A collection is a named container for documents, typically representing one kind of entity like users or orders.
Step 2
Implicit creation
Collections are created automatically the first time a document is inserted into them.
Step 3
Explicit options
You can also create a collection explicitly with options like schema validation or a capped size.
Step 4
Per-collection indexes
Indexes, including the default on _id, are defined and managed at the collection level.
Step 5
No forced shared schema
Documents inside a collection are not required to share identical fields, unlike rows in a SQL table.
What Interviewer Expects
- Defines a collection as a named group of documents
- Compares it to a table, noting the schema difference
- Knows collections can be created implicitly or explicitly
- Mentions indexes and validation rules live at the collection level
- Understands one database can hold many collections
Common Mistakes
- Treating a collection as requiring a fixed schema like a table
- Forgetting a collection is created on first insert automatically
- Confusing a collection with a database
- Assuming every document in a collection must be identical in shape
Best Answer (HR Friendly)
“A collection is a named group of related records in MongoDB, similar to a table in a traditional database. It's how MongoDB organizes data of the same kind, such as all customer records or all order records, in one place.”
Code Example
// Implicit creation on first insert
db.orders.insertOne({ orderId: "ORD-2001", total: 89.5 });
// Explicit creation with validation
db.createCollection("reviews", {
validator: {
$jsonSchema: {
required: ["rating", "productId"],
properties: { rating: { bsonType: "int", minimum: 1, maximum: 5 } }
}
}
});
db.orders.find({ total: { $gt: 50 } });
// => [{ _id: ObjectId("..."), orderId: "ORD-2001", total: 89.5 }]Follow-up Questions
- How does a collection differ from a relational table?
- How do you enforce schema validation on a collection?
- What is a capped collection and when would you use one?
- Can a single database contain multiple unrelated collections?
- How are indexes scoped to a collection versus a database?
MCQ Practice
1. What is a MongoDB collection most analogous to in SQL?
A collection groups documents the way a table groups rows, though without a rigid shared schema.
2. When is a collection typically created if not made explicitly?
MongoDB creates a collection implicitly the first time a document is inserted into it.
3. Where are indexes defined in MongoDB?
Indexes are created and managed per collection to speed up queries on that collection's documents.
Flash Cards
What is a MongoDB collection? — A named grouping of documents within a database, similar to a table.
When is a collection created by default? — Implicitly, on the first document insert, unless created explicitly with options.
Do documents in a collection need the same fields? — No — MongoDB does not force a single shared schema across documents.
What can be set at collection creation time? — Options like schema validation rules or a capped size limit.