Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a document store such as MongoDB, and when would you store an order with its line items as one document instead of two relational tables? What are the limits of embedding?

A document store such as MongoDB keeps self-describing documents, JSON-like records stored as BSON, each addressed by an _id, and it can index and query any field, including nested fields and arrays. We embed line items inside the order document when they are always read and written together with the order, their number is bounded, and they are not shared, because then one read returns the whole order and one atomic write changes it. Embedding stops working when documents approach the 16 MB limit, when arrays grow without bound, or when duplicated data must be updated in many places, and then we reference instead.

Why this question is asked

Most Java developers learn data modelling with relational tables, where an order and its lines are two tables joined by a foreign key. Document stores ask us to think differently: model data the way the application reads it. Interviewers ask this question to check that we understand both the benefit of that shift and where it breaks down, because a wrong embedding decision is painful to undo once millions of documents exist. In this answer, we will recap what a document store is, compare the relational and the embedded order, see why single-document atomicity matters, look at the limits of embedding, and decide when to reference instead.

1A Quick Recap of Document Stores

A document store saves records as documents, nested structures of fields, arrays and sub-documents, much like a JSON object. MongoDB stores them in a binary form called BSON, which adds types such as dates, 64-bit integers and decimals. Every document has a unique _id, and documents live in collections, which play the role tables play in a relational database.

Self-describing means each document carries its own field names, so two documents in the same collection can have different fields. That flexibility is useful, but it does not mean anything goes: we still create secondary indexes on the fields we query, and MongoDB can index nested fields with dot notation, such as lines.sku, and arrays, where it creates one index entry per element (a multikey index).

2The Same Order in Two Models

In a relational database the order has a row in orders and one row per line in order_lines. Reading an order with its lines means a join, or two queries. In a document store we can put the lines inside the order.

Java
// Spring Data MongoDB: the order and its lines are one document
@Document("orders")
public record Order(
@Id String id,
long customerId,
Instant placedAt,
String status,
List<LineItem> lines, // embedded, not a separate collection
BigDecimal total) {}
public record LineItem(String sku, String name, int qty, BigDecimal unitPrice) {}
Order o = orders.findById("ord-987").orElseThrow(); // one read
o.lines().size(); // 3, no join needed
// The stored document:
// { _id: "ord-987", customerId: 123, placedAt: ISODate("2026-03-01T10:00:00Z"),
// status: "PAID", total: Decimal128("59.90"),
// lines: [ { sku: "BK-1", name: "Java Book", qty: 1, unitPrice: Decimal128("39.90") }, ... ] }

Notice that the document matches the shape the application uses: the order page, the confirmation email and the invoice all need the order with its lines. That is the main argument for embedding. One read by _id returns everything, from one place on disk, without a join.

3When Embedding Is the Right Choice

There are three conditions, and an order's line items usually meet all of them. First, the children are read and written together with the parent: we almost never load one line without its order. Second, the number of children is bounded: an order has a handful of lines, perhaps a few hundred in an extreme case, not millions. Third, the children are not shared: a line item belongs to exactly one order, so there is only one copy to keep correct.

There is also a consistency benefit. In MongoDB a write to a single document is atomic, even when it changes several fields and array elements, so adding a line and updating the total in one update can never leave the order half-changed. MongoDB also supports multi-document transactions, since version 4.0 on replica sets and 4.2 on sharded clusters, but they cost more: they hold resources, add latency and have time limits. A good model makes most operations single-document.

Step throughEmbed or reference the child data?
  1. Is it read and written together with the parent?
    YesNext questionNoReference it
  2. Is the number of children bounded and small?
    YesNext questionNoReference or bucket it
  3. Is it shared by many parents or queried alone?
    YesReference itNoEmbed it

3 steps in order. Play them, or step through them one at a time.

Line items pass all three checks; products fail the last one.

4The Limits of Embedding

The first limit is hard: a MongoDB document can be at most 16 MB. An order never gets close, but a document that collects things over time can. The second limit is softer and comes earlier: unbounded arrays. A customer document with an embedded array of every order they ever placed grows forever. Each update writes a bigger document, the multikey index on that array grows with it, and every read of the customer drags all that history along even when we need only the name.

The third limit is duplication. If we embed data that also exists elsewhere, such as the full product description inside every order, then a change to that data has to be applied in many documents. Sometimes that duplication is exactly what we want, as we will see in a moment, but when the copies must stay in sync it becomes a maintenance burden and a source of inconsistency.

5When to Reference Instead

We reference, storing an id rather than the data, when the child is large, when it is shared by many parents, or when it is queried on its own. Products are the classic example: thousands of orders refer to the same product, the product has its own pages and searches, and it changes independently. So the product lives in a products collection, and the line item holds its sku.

Customers are another case. The order stores customerId, and the customer's orders are found with a query on an index over customerId rather than an array on the customer document. That keeps both documents bounded no matter how many orders a customer places.

6Follow-up questions

How would you model product details that change after an order?

The order should record what the customer actually bought, so each line item keeps a snapshot of the details at purchase time, such as the product name, unit price and tax rate, plus the sku as a reference to the live product. If the price changes tomorrow, the old order still shows the price that was paid, which is what invoices and refunds need. Anything that should always be current, such as the product image or stock level, is read from the products collection through the sku. Here duplication is deliberate: the snapshot is history, not a copy that must be kept in sync.

Is a document store schema-less?

Not really. The database does not force one schema, but the application still expects certain fields and types, so the schema lives in code. MongoDB can also enforce rules with a $jsonSchema validator on a collection, and when the shape changes we still need a migration plan, either rewriting documents or reading several versions.

What happens if an order has thousands of line items, such as a wholesale order?

If a few orders could hold tens of thousands of lines, embedding all of them risks very large documents. We can keep a summary and the first lines in the order and move lines into a separate collection with an index on orderId for those rare cases, or put the lines into bucket documents of a few hundred each.

7Conclusion

In this answer, we saw that a document store keeps self-describing BSON documents addressed by _id, with indexes on any field including nested ones. Embedding line items in the order works because they are read and written together, bounded and not shared, which gives one-read access and atomic single-document writes. Embedding breaks down near the 16 MB document limit, with arrays that grow forever, and with duplicated data that must stay in sync, so we reference large, shared or independently queried data such as products and customers.

What to say in the interviewAbout 80 seconds aloud

Open with

A document store like MongoDB stores records as JSON-like documents, in BSON, each with an _id, in collections. Documents can differ in shape, and we can index and query any field, including nested fields and array elements.

Then, if they want more

For an order I would usually embed the line items, because they are always read and written with the order, an order has a bounded number of lines, and a line belongs to one order only.

Then one read by id returns the whole order and an update that adds a line and changes the total is atomic, since single-document writes are atomic in MongoDB. Multi-document transactions exist but are more expensive, so I design to avoid needing them.

The limits are the 16 MB document size, arrays that grow without bound, which make every update and read heavier and grow multikey indexes, and duplicated data that has to be updated in many documents. So I reference data that is large, shared or queried on its own, like products and customers.

For product details that change, each line keeps a snapshot of name and price at purchase time plus the sku to look up current information.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation, MongoDB manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a document store such as MongoDB, and when would you store an order with its...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/5834b99a38/