knaxuslearn · build · ship

Projects / Product catalog

MongoDB · about 10 hours · 4 phases

Build a product catalog in MongoDB

When we shop on a website, every product has its own page, and the pages do not look alike. A laptop lists memory and storage, a shoe lists sizes, a book lists pages. Where do we keep things that differ this much, and how do we still search, sort and total them quickly? In about 10 hours we will build the answer ourselves: a product catalog in MongoDB that accepts flexible documents but still refuses bad ones, sells the last item only once, stays fast on 200,000 products and produces a sales report.

You work in four phases of about 2.5 hours each. Every phase ends with something you can run and see, so you always know whether you are done. By the end you will have:

  • a collection that rejects five kinds of bad documents on its own,
  • queries and updates that change one field of a document without touching the rest,
  • twenty buyers racing for ten laptops, and exactly ten of them winning,
  • a 200,000 product collection where indexes cut the work a query does from the whole collection to a few dozen documents,
  • a sales report built with an aggregation pipeline and checked two other ways.

You only need to know what a database table is and how to read a little JavaScript. Everything else is explained when it first shows up.

The four phases

  1. Documents that refuse bad data

    You end with five bad documents, and the collection refuses all five by itself.

    • 1.1 Two products that look different20 min
    • 1.2 Ask for them back20 min
    • 1.3 Add rules35 min
    • 1.4 No two products with the same sku15 min
    • 1.5 Try to break it40 min
  2. Ask questions, change one thing

    You end with twenty buyers racing for ten laptops, and exactly ten of them winning.

    • 2.1 Ten more products25 min
    • 2.2 Queries that read the way you think40 min
    • 2.3 Change one field, not the whole document35 min
    • 2.4 The oversell25 min
    • 2.5 Let the database do the check25 min
  3. Fast and searchable on 200,000 products

    You end with a 200,000 product collection, and the numbers that show what three indexes did.

    • 3.1 Load 200,000 products30 min
    • 3.2 Measure before you fix anything30 min
    • 3.3 One index, then a better one40 min
    • 3.4 Search by words30 min
    • 3.5 What the indexes cost20 min
  4. A sales report with an aggregation pipeline

    You end with a revenue report that you checked three different ways, and it matches every time.

    • 4.1 Twenty thousand orders25 min
    • 4.2 Your first pipeline40 min
    • 4.3 Join the product, group by category35 min
    • 4.4 Revenue by month20 min
    • 4.5 Put the filter first, and give it an index25 min
    • 4.6 Check the report three ways25 min

Start with the setup →