Aggregation in MongoDB works like Unix pipeline, one stage will pass to next stage.


$match works just like .find


$project is used to select a subset of the field, 0 means exclude and 1 means include.

We can map value using the syntax:

{$project: {
"newFieldName": "$firstLevel.second_level"
{$project: {
"newFieldName": "$firstLevel.second_level"

Metaphorically, $project is like


Split single documents into multiple based on a specific field with array.

Array expression

  • use $filter to filter items in an array as part of $project operation.
  • use $arrayElemAt to get item at specific index. (negative to get from end)
  • use $slice to get subset by index
  • use $size operator to get array length


Accumulator examples:

  • $sum
  • $avg
  • $first
  • $last
  • $max
  • $min
  • $mergeObjects
  • $push
  • $addToSet

We can use accumulator in group phase or project phase. When use in project phase, the accumulator can only be applied to array field.

Thanks for reading!

Love what you're reading? Sign up for my newsletter and stay up-to-date with my latest contents and projects.

    I won't send you spam or use it for other purposes.

    Unsubscribe at any time.