Let's think about this for a second
As an application's data grows, you need indexes to keep query speed under control. But it's not just about fixing up a single task list — plenty of apps also need a dashboard or report feature that summarizes things like how many tasks exist per status, or per priority. In this stage, we'll build an index on the status field — since it's frequently queried — and write an aggregation pipeline using $match and $group to produce a task-count summary report by status. This is where we bring together everything you've learned, from the Basic chapter all the way through the Advanced chapter, into a single project.
Let's build it for real
Build an ascending index on the status field with createIndex({ status: 1 }) — this will speed up queries that filter by status. Next, write an aggregate() pipeline: use a $match stage to select only tasks where priority: "high", then a $group stage that takes the status field as _id and tallies the task count with $sum: 1, and finally add a $sort stage to order the results from highest count to lowest. The pipeline's result gives you a summary report of how many high-priority tasks exist per status — and that's the final step of this project.
Code Example
// status field ပေါ်မှာ index တည်ဆောက်ပါ (query speed အတွက်)
db.tasks.createIndex({ status: 1 })
// index list ကို confirm လုပ်ကြည့်ပါ
db.tasks.getIndexes()
// priority: high task များကို status အလိုက် count လုပ်၍ report ထုတ်ပါ
db.tasks.aggregate([
{ $match: { priority: "high" } },
{ $group: { _id: "$status", total: { $sum: 1 } } },
{ $sort: { total: -1 } }
])Once the aggregate() pipeline runs, it returns summary documents with counts per status, like { _id: "pending", total: 2 }, { _id: "completed", total: 1 }.5-Minute Try-It
Rewrite the $group stage to use the priority field instead, and produce a task-count summary report by priority as well — take 5 minutes to update the pipeline and run it.
A quick word of caution
When writing an aggregation pipeline, put the $match stage as early as possible — the sooner you can cut down the number of documents, the faster the whole pipeline runs.