Skip to content
Learn Netverks

Lesson

Step 25/36 69% through track

match-group-project

$match, $group, and $project

Last reviewed Jun 1, 2026 Content v20260601
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches $match, $group, and $project: document modeling, query operators, and aggregation patterns for MongoDB.

Teams query $match, $group, and $project on every MongoDB codebase—skipping it leaves gaps in debugging and data reviews.

You will apply $match, $group, and $project in contexts like: Content catalogs, event logs, mobile sync backends, and polyglot stacks beside SQL services.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Core trio: filter rows, aggregate metrics, shape output fields.

Group with accumulators

db.orders.aggregate([
  { $match: { status: { $in: ['shipped', 'delivered'] } } },
  { $group: {
      _id: { year: { $year: '$shippedAt' }, customerId: '$customerId' },
      orders: { $sum: 1 },
      revenue: { $sum: '$total' }
  }},
  { $project: { customerId: '$_id.customerId', year: '$_id.year', orders: 1, revenue: 1, _id: 0 } }
])

Practice: Run aggregation pipelines in mongosh.

Accumulators

$sum, $avg, $max, $push, $addToSet—pick by report needs.

Important interview questions and answers

  1. Q: $push vs $addToSet?
    A: $push keeps duplicates; $addToSet unique values per group.
  2. Q: $project after $group?
    A: Flattens nested _id for API-friendly JSON.

Self-check

  1. What does $sum: '$total' do?
  2. Difference $push vs $addToSet?

Tip: $addToSet when you need unique values per group; $push keeps duplicates.

Interview prep

$group _id?
Grouping key; null groups all docs.
$project role?
Shapes output fields after group.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $addToSet when?
  • $project flatten?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump