Thuta Learning
ProjectsData & Databasesintermediate

Mini Project: Task Manager Database — Part 3 (Indexes, Aggregation, and Summary Reports)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Project: Task Manager Database — Part 3 (Indexes, Aggregation, and Summary Reports) in a real, hands-on project
  • Write and run the code yourself
  • Build an entire project step by step

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

javascript
// 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 } }
])
You should see
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.

Easy traps

  • Forgetting to put a $ in front of the field name inside the $group stage's _id (it should be $status), which throws an error
  • Putting $group before $match in the pipeline order — grouping every document before filtering — which hurts performance

Now Try It Yourself

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.

You'll know it worked when: Once the aggregate() pipeline runs, it returns summary documents with counts per status, like { _id: "pending", total: 2 }, { _id: "completed", total: 1 }.