Projects / Product catalog / Wrap up
Wrap up
Keep going
Pick one when you want more:
- Transactions. Start MongoDB as a one-member replica set, then move stock and an order in one multi-document transaction.
- Schema design. Redo the orders as a separate document per item, and compare it with the embedded list. Which queries got easier, and which got harder?
- Search. Compare
$textwith MongoDB's separate full-text search product, and with a plain regex query. - Time to live. Add a collection of carts with a TTL index, so abandoned carts delete themselves.
- Change streams. Watch the catalog for changes and print each price change as it happens.
What this version leaves out, and what a real catalog does
We kept the ideas and dropped the weight.
- Our single container is a standalone server. Real deployments run a replica set, copies of the data on several machines, so one machine dying does not take the catalog with it. Multi-document transactions and change streams need a replica set.
- Real catalogs shard very large collections across machines. We never needed to.
- A document can be at most 16 MiB. Embedding something that grows without limit, such as every review of a product, would eventually break that. Real schemas decide what to embed and what to keep in its own collection.
- Real systems version their documents, with a field such as
schema_version, so old and new shapes can live side by side while a migration runs. $textis simple. Real shops that need typo tolerance, facets and ranking by popularity use a dedicated search engine or MongoDB's full-text search product.- We handled concurrency with one atomic update. Real order flows also need idempotency keys, so a retry cannot buy twice.
Check yourself
Answer each question in your head first, then open it to check.
Why can one collection hold a laptop and a shoe, and what stops it from holding nonsense?
Documents in a collection do not have to share fields, so each product can have its own. A validator written in JSON Schema checks every insert and update against the few rules that must always hold, and a unique index covers rules that need to look at other documents.A validator exists, yet a duplicate sku still got through in some other project of yours. What else would you add?
A unique index on the field. A validator sees one document at a time and cannot know about the others.In Step 2.2, why did the dot-notation query return more products than
$elemMatch?With dot notation, each condition on the array can be satisfied by a different element, so a product with size 42 in one variant and no stock in another matched.$elemMatchrequires both conditions to hold for the same element.Why did twenty buyers cause a mistake in the first version of Step 2.4 and not in the second?
The first version read the stock and wrote it back in two separate steps, so many buyers read the same number before any wrote. The second version checked and changed the stock in one atomic update on one document.What did the order of the fields in the compound index in Step 3.3 decide?
Which questions the index can answer without extra work. Equality fields first let it jump to one slice, the sort field next gives the right order inside the slice, and the range field last narrows it. A different order would fit a different set of queries.Teach it back: explain to a teammate why a
$matchat the start of a pipeline is faster than one at the end.Each stage passes its output to the next, so a$matchat the start throws away documents before they travel through$unwind,$groupand the rest, and it can use an index. At the end, all the work has already been done on documents that are then discarded.