NoSQL · Data models beyond tables
How do document databases store and query data?
Short answer
Document databases store self-contained JSON-like documents, usually grouped into collections. You query by fields inside the document, and the model favours reading and writing an aggregate in one operation.
A document is a tree of fields, arrays and nested objects. It is stored and fetched as a unit, so embedding related data avoids a join. Documents have an identity, commonly an _id field, and live in a collection that is roughly the equivalent of a table.
db.products.insertOne({
_id: "sku_1",
name: "Mechanical Keyboard",
price: 4999,
tags: ["keyboard", "wireless"],
dimensions: { width: 320, depth: 130, height: 30 },
reviews: [
{ user: "asha", rating: 5, text: "Great feel" },
{ user: "ravi", rating: 4, text: "A bit loud" }
]
});
db.products.find({ tags: "wireless", price: { $lt: 6000 } });What the model is good at
- Fetching a complete aggregate, such as an order with its line items
- Flexible attributes that differ between documents in the same collection
- Atomic updates within a single document
- Indexes on nested fields and array elements
The main physical constraint is document size, which is 16 MB in MongoDB. Unbounded arrays are therefore a modelling bug: a document that grows forever will eventually hit the limit and become expensive to rewrite on every update.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you split a document into multiple collections?
- 2What happens when an embedded array grows without bound?
- 3How do indexes work on array fields?
- 4Are document databases schemaless?
Say it in the interview
A document store keeps a JSON-like aggregate together, so reading an order with its items is one lookup instead of a join. I model around the aggregate and its access patterns, embedding data that is read together and referencing data that is shared or unbounded. The physical limit matters, so I never let arrays grow forever. Multi-document transactions exist, but I treat them as a small safety net rather than the main modelling tool.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.