Day 3 of 5
⏱ ~60 minutes
MongoDB in 5 Days — Day 3

Aggregation Pipelines

Transform and analyze data with $match, $group, $sort, $lookup, and $project stages.

Aggregation Pipeline Concept

The aggregation pipeline is like a series of Unix pipes. Each stage transforms the documents and passes results to the next stage.

Pipeline stages
db.orders.aggregate([
  // Stage 1: filter documents
  { $match: { status: 'completed' } },

  // Stage 2: group and calculate
  { $group: {
    _id: '$category',
    totalRevenue: { $sum: '$amount' },
    orderCount: { $count: {} },
    avgOrder: { $avg: '$amount' }
  }},

  // Stage 3: sort results
  { $sort: { totalRevenue: -1 } },

  // Stage 4: limit
  { $limit: 10 },

  // Stage 5: reshape output
  { $project: {
    _id: 0,
    category: '$_id',
    totalRevenue: 1,
    orderCount: 1,
    avgOrder: { $round: ['$avgOrder', 2] }
  }}
])
$lookup: Joins
// Join users to their orders
db.orders.aggregate([
  { $lookup: {
    from: 'users',
    localField: 'userId',
    foreignField: '_id',
    as: 'user'
  }},
  { $unwind: '$user' },  // flatten the user array
  { $project: {
    orderId: '$_id',
    amount: 1,
    userName: '$user.name',
    userEmail: '$user.email'
  }}
])
📝 Day 3 Exercise
Build a Sales Report
  1. U
  2. s
  3. i
  4. n
  5. g
  6. y
  7. o
  8. u
  9. r
  10. p
  11. r
  12. o
  13. d
  14. u
  15. c
  16. t
  17. s
  18. a
  19. n
  20. d
  21. o
  22. r
  23. d
  24. e
  25. r
  26. s
  27. c
  28. o
  29. l
  30. l
  31. e
  32. c
  33. t
  34. i
  35. o
  36. n
  37. s
  38. ,
  39. w
  40. r
  41. i
  42. t
  43. e
  44. a
  45. p
  46. i
  47. p
  48. e
  49. l
  50. i
  51. n
  52. e
  53. t
  54. h
  55. a
  56. t
  57. :
  58. f
  59. i
  60. l
  61. t
  62. e
  63. r
  64. s
  65. t
  66. o
  67. c
  68. o
  69. m
  70. p
  71. l
  72. e
  73. t
  74. e
  75. d
  76. o
  77. r
  78. d
  79. e
  80. r
  81. s
  82. ,
  83. g
  84. r
  85. o
  86. u
  87. p
  88. s
  89. b
  90. y
  91. p
  92. r
  93. o
  94. d
  95. u
  96. c
  97. t
  98. c
  99. a
  100. t
  101. e
  102. g
  103. o
  104. r
  105. y
  106. ,
  107. c
  108. a
  109. l
  110. c
  111. u
  112. l
  113. a
  114. t
  115. e
  116. s
  117. t
  118. o
  119. t
  120. a
  121. l
  122. r
  123. e
  124. v
  125. e
  126. n
  127. u
  128. e
  129. a
  130. n
  131. d
  132. o
  133. r
  134. d
  135. e
  136. r
  137. c
  138. o
  139. u
  140. n
  141. t
  142. p
  143. e
  144. r
  145. c
  146. a
  147. t
  148. e
  149. g
  150. o
  151. r
  152. y
  153. ,
  154. s
  155. o
  156. r
  157. t
  158. s
  159. d
  160. e
  161. s
  162. c
  163. e
  164. n
  165. d
  166. i
  167. n
  168. g
  169. b
  170. y
  171. r
  172. e
  173. v
  174. e
  175. n
  176. u
  177. e
  178. .

Day 3 Summary

  • Aggregation pipelines are stages: $match$group$sort$project.
  • $group with $sum, $avg, $count, $min, $max for analytics.
  • $lookup joins collections. Follow with $unwind to flatten the joined array.
  • Put $match first so subsequent stages process fewer documents.
Finished this lesson?