What is a document and a collection in MongoDB?
BeginnerConceptMongoDB
Answer
Short answer
A document is a single record stored as a JSON-like object (BSON) with fields, nested objects and arrays. A collection is a group of documents — similar to a table, but documents in the same collection don't need identical fields.
Example
// collection: customers
{ _id: ObjectId("..."), name: "Asha", email: "[email protected]", address: { city: "Pune" } }
{ _id: ObjectId("..."), name: "Ravi", email: "[email protected]", phone: "+91-98...", tags: ["vip"] }
Both live in the same collection even though their fields differ.
Mapping to SQL
| SQL | MongoDB |
|---|---|
| database | database |
| table | collection |
| row | document |
| column | field |
Limits worth knowing
- Maximum document size: 16 MB.
- Every document has a unique
_id(MongoDB adds an ObjectId if you don't).
What to say in the interview
"A document is a BSON record with fields, nested objects and arrays; a collection groups documents without enforcing a fixed schema. It's roughly table and row in SQL terms, with a 16 MB document limit and a mandatory _id."
More on MongoDB
What is MongoDB and how is it different from MySQL or PostgreSQL?BeginnerHow do you run MongoDB, log in and work with data?BeginnerWhat is a NoSQL database?BeginnerWhat is BSON?BeginnerWhat is the _id field and what is an ObjectId?BeginnerHow do you connect to MongoDB and explore databases and collections?Beginner