Latticework

Command Palette

Search for a command to run...

MongoDB

Aggregation Pipeline

18 min

Explanation

MongoDB's aggregation pipeline chains multiple STAGES together, each one transforming the output of the previous stage — conceptually identical to a Unix pipe, or chaining pandas dataframe operations: filter, then group, then sort, each stage receiving what the last one produced.

def pipeline_match(documents, query):
    return [doc for doc in documents if all(doc.get(k) == v for k, v in query.items())]

employees = [
    {"dept": "eng", "salary": 100},
    {"dept": "sales", "salary": 80},
    {"dept": "eng", "salary": 120},
]
engineers = pipeline_match(employees, {"dept": "eng"})   # the $match stage
Try it

Real code would chain: pipeline_group_sum(pipeline_match(sales, {'amount': 100}), 'region', 'amount') -- match first, THEN group -- exactly mirroring how you'd write [$match, $group] as an actual MongoDB aggregation array.

Loading editor…
Explanation

$group is the aggregation pipeline's most powerful stage — collapsing many documents into one summary document per distinct group value, exactly like SQL's GROUP BY from the SQL course:

def pipeline_group_sum(documents, group_field, sum_field):
    result = {}
    for doc in documents:
        key = doc[group_field]
        result[key] = result.get(key, 0) + doc[sum_field]
    return result

print(pipeline_group_sum(sales, "region", "amount"))
# {'west': 250, 'east': 200}

Real MongoDB aggregation supports far more accumulators than just sum ($avg, $min, $max, $count, $push to collect values into an array) — but every one of them follows this same "one accumulated value per group" shape.

Exercise

Write `pipeline_match(documents, query)`: a `$match`-stage equivalent — return every document matching all key/value pairs in `query`.

Exercise

Write `pipeline_group_sum(documents, group_field, sum_field)`: a `$group`-stage equivalent — return a dict mapping each distinct value of `group_field` to the SUM of `sum_field` across documents sharing that value.

Quiz

What does MongoDB's aggregation pipeline let you do?

Checkpoint

You can implement $match and $group aggregation stages, and understand how chaining stages together builds up complex queries from simple pieces.