Mongodb Guide

$group Stage in MongoDB Aggregation Pipeline

1. Definition

The $group stage is used to group documents based on a specified field and perform aggregate calculations like count, sum, average, etc.

2. Simple Understanding

👉 It works like GROUP BY in SQL
👉 It groups similar data and performs calculations on them

3. When to Use

  • When summarizing data
  • When counting records
  • When calculating totals or averages
  • When creating reports

4. Where it is Used

  • Analytics dashboards
  • Sales reports
  • User statistics
  • Backend data processing

5. Why We Use It

  • To summarize large data
  • To perform calculations in database
  • Reduces need for external processing
  • Efficient for analytics

6. How It Works

  • Groups documents using _id field
  • Applies aggregation operators
  • Outputs one document per group
  • Supports operators like $sum, $avg, $max, $min

7. Basic Syntax

db.collection.aggregate([
  {
    $group: {
      _id: "$fieldName",
      total: { $sum: 1 }
    }
  }
]);

8. Real Example

// Count users by role
db.users.aggregate([
  {
    $group: {
      _id: "$role",
      count: { $sum: 1 }
    }
  }
]);

// Calculate total salary by department
db.employees.aggregate([
  {
    $group: {
      _id: "$department",
      totalSalary: { $sum: "$salary" }
    }
  }
]);

9. Explanation

  • _id defines grouping key
  • $sum: 1 counts documents
  • Can calculate totals, averages, etc.
  • Returns aggregated result per group

10. Common Operators

  • $sum → Total or count
  • $avg → Average value
  • $max → Maximum value
  • $min → Minimum value

11. Advantages

  • Powerful data aggregation
  • Reduces application logic
  • Efficient for large datasets

12. Disadvantages

  • Can be memory-intensive
  • Complex queries may affect performance

Interview Points

  • $group = GROUP BY in SQL
  • Uses _id as grouping key
  • Supports aggregation operators
  • Used for analytics and reporting