Computed Pattern for Pre-Aggregation in MongoDB
📖 Scenario: You work for a small online bookstore. You want to keep track of the total number of books sold per author to quickly show popular authors without counting every sale each time.
🎯 Goal: Build a MongoDB collection that stores book sales and a pre-aggregated collection that keeps the total sales per author updated using computed fields.
📋 What You'll Learn
Create a
sales collection with documents containing book, author, and copies_sold fields.Create a variable
min_copies to filter sales with at least 5 copies sold.Write an aggregation pipeline that sums
copies_sold per author only for sales meeting the min_copies threshold.Create a
pre_aggregated_sales collection that stores the total copies sold per author.💡 Why This Matters
🌍 Real World
Pre-aggregation helps websites and apps show summary data quickly without recalculating totals every time a user requests it.
💼 Career
Database developers and data engineers often create pre-aggregated collections or tables to improve performance and user experience.
Progress0 / 4 steps