What is a document in MongoDB?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for MongoDB development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
A document is the basic unit of data in MongoDB, analogous to a row in a relational database. Documents are stored as BSON (Binary JSON) objects — ordered sets of key-value pairs. BSON supports all JSON types (string, number, boolean, array, object, null) plus additional types: ObjectId, Date, Binary, Decimal128, Int32, Int64, Timestamp, Regular Expression. Example document: { "_id": ObjectId("507f1f77bcf86cd799439011"), "name": "Alice Johnson", "email": "alice@example.com", "age": 30, "address": { "street": "123 Main St", "city": "New York" }, "hobbies": ["reading", "coding"], "createdAt": ISODate("2024-01-15T10:00:00Z") }. Key characteristics: (1) Every document must have a unique _id field (auto-generated ObjectId if not provided); (2) Documents can contain nested objects and arrays (embedded documents); (3) Documents in the same collection can have different sets of fields (flexible schema); (4) Maximum document size: 16MB. Documents directly map to objects in application code — no ORM/mapping layer needed. Related data can be embedded (avoiding joins) or referenced (using a foreign-key-like pattern).
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a MongoDB codebase.