Let's think about this for a second
This exercise combines what you learned in the Intermediate and Advanced chapters — query operators, indexes, and the aggregation framework — and puts them to work in trickier scenarios. Working with a single orders collection, you'll write comparison and logical operators, create an index, and build a multi-stage aggregation pipeline entirely on your own. Nail this exercise, and you'll have solid proof that you can use every core MongoDB skill with confidence.
Exercises
Task 1: Insert 5 documents into the orders collection with amount, status, customer, and createdAt fields using insertMany(). Task 2: Use the $or operator with find() to look up orders where status: "pending" or amount is greater than 100. Task 3: Build an index on the customer field using createIndex(). Task 4: Write an aggregation pipeline that groups orders where status: "completed" by customer, calculates each customer's total amount using $sum, and sorts the results from highest to lowest amount.
Code Example
// Task 1: sample orders data insert လုပ်ပါ
db.orders.insertMany([
{ customer: "Zin Mar", amount: 150, status: "completed", createdAt: new Date("2026-08-01") },
{ customer: "Htet Htet", amount: 80, status: "pending", createdAt: new Date("2026-08-05") },
{ customer: "Zin Mar", amount: 220, status: "completed", createdAt: new Date("2026-08-10") },
{ customer: "Kyaw Kyaw", amount: 60, status: "pending", createdAt: new Date("2026-08-12") },
{ customer: "Htet Htet", amount: 300, status: "completed", createdAt: new Date("2026-08-15") }
])
// Task 2: $or operator ဖြင့် query ရေးပါ (ဒီနေရာမှာ ရေးပါ)
// Task 3: customer field ပေါ်မှာ index တည်ဆောက်ပါ (ဒီနေရာမှာ ရေးပါ)
// Task 4: customer အလိုက် completed order amount စုစည်းသော aggregation pipeline ရေးပါ (ဒီနေရာမှာ ရေးပါ)After Task 4, you should get a sorted array of documents showing each customer's total amount for completed orders, like { _id: "Htet Htet", total: 300 }, { _id: "Zin Mar", total: 370 }.5-Minute Try-It
Take 5 minutes to add a $project stage to Task 4's pipeline so it only shows the total field.
A quick word of caution
The more complex an aggregation pipeline gets, the more it helps to debug it by checking each stage's output individually — either with explain() or by running the pipeline stage by stage.