Practice & Assessment
Test your understanding of Aggregation Pipeline
Multiple Choice Questions
5Why should $match appear as early as possible in an aggregation pipeline?
A post document has tags: ['js', 'node', 'mongo']. After a $unwind on 'tags', how many documents are produced from this one document?
What does $addToSet do in a $group stage?
What type of join does $lookup perform?
Which stage would you use to write aggregation results permanently into a different collection?
Coding Challenges
1Sales Dashboard Aggregation
Given an 'orders' collection with fields: userId, status (pending/completed/cancelled), total (number), category (string), createdAt (Date). Write a single aggregation pipeline using $facet that returns in one query: (1) total revenue grouped by category, sorted descending, (2) order count by status, (3) top 3 highest-value orders with userId and total. Print the result as formatted JSON. Input: seed the collection with at least 20 documents. Output: a single object with three arrays. Estimated time: 25 minutes.
Mini Project
Analytics API for an E-commerce Store
Build a Node.js + Express API with these analytics endpoints powered entirely by MongoDB aggregation pipelines: GET /analytics/revenue?period=monthly returns monthly revenue for the last 12 months. GET /analytics/top-products?limit=5 returns the 5 best-selling products by total quantity sold. GET /analytics/customers/:id/summary returns a specific customer's total orders, total spent, most-bought category, and first/last order date. Use $lookup to join the orders and products collections, $group, $project, $sort, $limit, and $bucket where relevant. Seed data must have at least 100 orders across 20 products and 10 users.
