Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

In a MongoDB order system, each order shows the product name and price at the time of purchase, while the product catalogue changes daily. How do you model products in orders, and what is the extended reference pattern?

Each order line should copy the product fields needed at purchase time, such as productId, name and unit price, into the order document; this is the extended reference pattern, a reference that carries a few frequently used fields along with the id. The copy is deliberately historical, because a price change tomorrow must not change what the customer paid today, and keeping productId still lets us join to the current product with $lookup. For any copied field that should stay current, such as a display name, we need an update strategy, so we decide field by field whether it is a snapshot or a live value.

Why this question is asked

Orders are the classic case where copying data is not a compromise but the correct model. In a relational schema we would normally store only product_id in the order line and join to products, and then discover that last month's invoices now show this month's prices. Interviewers ask this question to see whether we can reason about data over time, not just about structure, and whether we know the named pattern for it. In this answer, we will recap references and embedding in MongoDB, see what goes wrong with a plain reference, apply the extended reference pattern, look at how to still reach current product data, and finish with how to keep the few live fields in sync.

1A Quick Recap of References and Embedding

In MongoDB we can either embed related data inside a document or reference another document by its id. A plain reference is like a foreign key: the order line stores productId, and to show the product name we look it up in the products collection, either with a second query or with $lookup, the aggregation stage that joins another collection.

The extended reference pattern sits between the two. We store the reference id and also copy a small number of the referenced document's fields that we need most often. The order line then contains enough to be displayed and processed on its own, while the id still points to the full product.

2What Goes Wrong With a Plain Reference

Imagine the order stores only productId and quantity, and the price is read from the catalogue when we display the order.

Java
db.products.insertOne({ _id: "SKU-1", name: "Trail Shoe", price: NumberDecimal("89.00") });
db.orders.insertOne({ _id: 1001, lines: [ { productId: "SKU-1", qty: 2 } ] });
// next week the catalogue changes
db.products.updateOne({ _id: "SKU-1" },
{ $set: { name: "Trail Shoe v2", price: NumberDecimal("99.00") } });
// the customer opens order 1001 and the page joins to products
// shows: Trail Shoe v2, 2 x 99.00 = 198.00 (they paid 178.00)

The order now tells a lie. The customer paid 178.00 for two Trail Shoes, but the page shows 198.00 for a product name that did not exist when they ordered. Refunds, invoices, tax reports and disputes would all be computed from the wrong numbers. The problem is not MongoDB; it is that the price and name at purchase time are facts about the order, not about the product, and we did not store them in the order.

3Applying the Extended Reference Pattern

So we copy the purchase-time facts into each line, and keep the id.

Java
db.orders.insertOne({
_id: 1001,
customerId: 42,
createdAt: ISODate("2026-11-01T09:30:00Z"),
lines: [
{ productId: "SKU-1", // reference to the live product
name: "Trail Shoe", // snapshot at purchase time
unitPrice: NumberDecimal("89.00"), // snapshot at purchase time
qty: 2,
lineTotal: NumberDecimal("178.00") }
],
total: NumberDecimal("178.00")
});
db.orders.findOne({ _id: 1001 }); // displays correctly with no join, forever
Step throughPlacing an order with extended references
Checkout serviceproductsorders
find SKU-1: name, price
Trail Shoe, 89.00
build line with productId, name, unitPrice
insert order with copied fields
ok

5 messages in order. Play them, or step through them one at a time.

The copy happens once, at purchase time, from the catalogue value the customer saw.

Notice what we gain. Displaying an order needs one read with no join, which is ideal because order history pages are read far more often than products change. The order is a self-contained record of what happened, which is exactly what accounting and support need. And the price is stored as Decimal128, MongoDB's exact decimal type, so totals add up to the cent.

We copy only what the order needs, not the whole product. Copying the product description, images and stock level would bloat every order and invite confusion about which copy is right.

4Still Reaching the Current Product

Because we kept productId, we can always get current product data when a page actually needs it, for example a 'buy again' button that should show today's price and availability.

Java
db.orders.aggregate([
{ $match: { _id: 1001 } },
{ $unwind: "$lines" },
{ $lookup: { from: "products", localField: "lines.productId",
foreignField: "_id", as: "current" } },
{ $project: { name: "$lines.name", paid: "$lines.unitPrice",
nowPrice: { $first: "$current.price" } } }
]);
// { name: "Trail Shoe", paid: 89.00, nowPrice: 99.00 }

So the order shows what was paid, and the buy-again widget shows what it costs now. We have both truths, each in its proper place, and the join only runs on the pages that need the live value.

5Snapshot or Live: Deciding Per Field

The pattern raises one design question for every copied field: should it stay frozen or follow the source? Price and the name printed on the invoice are snapshots, and must never change. But suppose we also copy a product's display category or a seller's shop name into orders, so that an order list can be filtered or shown without a join. If the seller renames the shop, we may want old orders to show the new name.

For live fields we need an update strategy. One option is a change stream, MongoDB's feed of changes to a collection, consumed by a small service that updates the copies when the source changes. Another is a nightly batch job. Both accept some staleness, seconds or hours, and we should say how much is acceptable. We also keep the number of live copied fields small, because each one is a write fan-out: renaming a shop with two million orders means two million updates.

Java
// A Java consumer that updates a live copied field when a seller is renamed
sellers.watch(List.of(Aggregates.match(Filters.eq("operationType", "update"))))
.fullDocument(FullDocument.UPDATE_LOOKUP)
.forEach(change -> {
Document seller = change.getFullDocument();
orders.updateMany(Filters.eq("sellerId", seller.get("_id")),
Updates.set("sellerName", seller.getString("name")));
});
Snapshot or live for each copied field
Field in the order lineKindReason
unitPriceSnapshotwhat the customer paid, legal and accounting record
name at purchaseSnapshotwhat the invoice said
taxRateSnapshottax applied at that time
sellerNameLive, updated by change streamshop renames should show everywhere
stock levelDo not copychanges constantly, look it up when needed

In production this consumer must also store its resume token so it can continue after a restart without missing changes, and the batch update should run in pages for very large fan-outs.

6Follow-up questions

Is duplicating data not a violation of good design?

Normalisation rules aim to avoid inconsistent copies of the same fact. The purchase price is a different fact from the current price, so storing both is not duplication of one fact. Relational designs do the same: a good order_lines table also stores unit_price.

What if the product is deleted?

The order still displays correctly because it holds its own copy of name and price. The $lookup for current data returns an empty array, and the page simply hides the buy-again option. That robustness is another benefit of the pattern.

How many fields should we copy?

Only the ones needed by the most frequent reads of the order, typically id, name, price and perhaps a SKU or image URL. If we find ourselves copying many fields that must stay live, embedding or a plain reference is probably the better model.

7Conclusion

In this answer, we saw that a plain product reference makes past orders show today's prices, because the purchase-time name and price are facts about the order. The extended reference pattern copies those few fields into each order line while keeping productId, so orders display with one read and stay historically correct, and $lookup still reaches current product data when needed. Any copied field that should stay current needs an update strategy, such as a change stream or a batch job with accepted staleness, so we decide per field whether it is a snapshot or live.

What to say in the interviewAbout 80 seconds aloud

Open with

I would store each order line with the productId plus a copy of the fields that describe the purchase: the product name, the unit price as Decimal128, the quantity and the line total.

Then, if they want more

That is the extended reference pattern, a reference that carries a few frequently used fields of the referenced document. Here the copy is intentionally historical. If the catalogue changes the price tomorrow, the order must still show what the customer paid, and a plain reference with a join would show today's price and break invoices, refunds and support.

With the copied fields an order displays with one read and no join. I keep productId so I can still use a lookup when a page needs live data, like a buy again button showing the current price. Then I decide field by field whether a copy is a snapshot or a live value.

Price, invoice name and tax rate are snapshots and never change. If I copy something that should follow the source, like a seller's display name, I need an update strategy, typically a change stream consumer with a resume token, or a nightly batch, and I state how much staleness is acceptable. I keep live copied fields few, because each one is a write fan-out.

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
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 "In a MongoDB order system, each order shows the product name and price at the time of...": https://scrivio-interview-library.netlify.app/databases/mongodb/4f7622386b/