What is a document and a collection in MongoDB?
Understand MongoDB documents and collections — how BSON records map to rows, how collections group them, and why the schema stays flexible.
Expected Interview Answer
In MongoDB, a document is a single record stored as a set of field-and-value pairs in BSON (binary JSON), and a collection is a group of related documents — roughly the equivalents of a row and a table in relational databases.
A document is the basic unit of data: an ordered set of keys mapped to values, where values can be strings, numbers, arrays, or nested sub-documents, and every document gets a unique _id field. A collection holds many such documents but does not enforce a fixed schema, so documents within it can have different fields. Collections live inside a database, and MongoDB creates them implicitly on the first insert.
- Documents map naturally to application objects
- Collections group related data without a rigid schema
- Nested arrays and sub-documents avoid extra JOINs
- Each document is uniquely identified by _id
- Collections are created automatically on first insert
AI Mentor Explanation
A document is like one player's complete profile card — name, stats, and the list of matches played all on that single card. A collection is the whole team's binder holding every player's card together. One card can list bowling figures another card leaves out, yet they all sit in the same binder for the squad.
Step-by-Step Explanation
Step 1
Define a document
A document is a BSON record of field-value pairs, like { name: 'Asha', age: 30 }, with a unique _id.
Step 2
Note allowed value types
Values can be strings, numbers, booleans, dates, arrays, or nested sub-documents.
Step 3
Define a collection
A collection is a named group of documents, the analog of a relational table.
Step 4
Understand flexible schema
Documents in one collection need not share the same fields or structure.
Step 5
Know implicit creation
MongoDB creates the collection (and database) automatically on the first insert.
What Interviewer Expects
- Document defined as BSON field-value pairs with an _id
- Collection defined as a group of documents
- The row-to-document and table-to-collection mapping
- Awareness that collections are schema-flexible
- Knowing collections are created implicitly on first insert
Common Mistakes
- Calling a collection a database
- Saying all documents in a collection must have identical fields
- Forgetting the automatic _id field on every document
- Confusing a document with a single field rather than a full record
Best Answer (HR Friendly)
“A document in MongoDB is a single record that keeps related information together, like one customer's full details. A collection is simply a group of those records — the way a folder holds many files.”
Code Example
// 'books' is the collection; the object is one document
db.books.insertOne({
_id: 101, // unique identifier for this document
title: "Designing Data-Intensive Applications",
author: "Martin Kleppmann",
tags: ["databases", "systems"],
meta: { pages: 616, published: 2017 } // nested sub-document
})
// The collection is created automatically on the first insert
db.books.find({ author: "Martin Kleppmann" })Follow-up Questions
- What is the _id field and how is it generated?
- Can documents in the same collection have different fields?
- What is the maximum size of a BSON document?
- How do capped collections differ from normal ones?
- How do you model relationships between documents?
MCQ Practice
1. In MongoDB, a document is most like which relational concept?
A document corresponds to a single row/record; a collection corresponds to a table.
2. Which field uniquely identifies every MongoDB document?
Every document has an _id field, generated automatically as an ObjectId if not supplied.
3. When is a collection created in MongoDB?
MongoDB creates a collection implicitly the first time a document is inserted into it.
Flash Cards
What is a document? — A single BSON record of field-value pairs with a unique _id.
What is a collection? — A named group of related documents — the analog of a relational table.
Must documents share a schema? — No — a collection is schema-flexible, so documents can differ.
How is a collection created? — Implicitly, on the first document insert.