Sikau

MongoDB · Day 15 · about 45 min

Capstone: Sales dashboard pipeline

One pipeline for a whole dashboard card.

The dashboard card

The owner's dashboard shows, per day, the number of served orders, revenue, and the best-selling dish. Build it as one aggregation pipeline.

Plan the pipeline

The dashboard card needs, per day, the number of served orders, the revenue and the top dish. That's two levels of grouping: first by day and dish to get each dish's quantity and revenue per day, then by day to total everything up and pick the best dish. Sorting by quantity before the second group lets $first pick the winner.

  • $match served orders.
  • $addFields day with $dateToString in Kathmandu time.
  • $unwind items.
  • $group by day + dish: quantity, revenue, order ids.
  • $sort by day, then quantity descending.
  • $group by day: orders, revenue, top dish with $first.
  • $project the final shape and $sort by day.

Write and check the first group before adding the second.

The rest of this lesson

  • Counting orders, not linesexplain
  • Verify against the raw dataexplain
  • Stages in orderorder
  • Operator for each needmatch
  • Daily summarychallenge
  • Badge quizquiz
  • MongoDB complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of MongoDB