Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
The aggregation pipeline is MongoDB's answer to SQL GROUP BY, WHERE, and SELECT — but composable as a sequence of stages. Each stage transforms the stream of documents: $match filters, $group buckets and computes accumulators, $project reshapes. Running aggregations server-side instead of pulling documents into application code is often a 100× throughput improvement on analytical queries, because you move computation to where the data lives rather than shuffling gigabytes across the network.
Count orders by status and compute total revenue per status using $match, $group, and $project.
$match stage before $group to filter only orders from the last 30 days using a createdAt date field. Verify the revenue totals change by inserting a few old-dated orders.$addFields stage after $group that computes avgOrderValue as totalRevenue / count. Confirm the arithmetic is correct against manually computed values.$match stage on a collection of 10,000 documents. Use explain('executionStats') to compare nReturned and totalDocsExamined at the $group stage.package main
import (
"context"; "fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
client, _ := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
col := client.Database("demo").Collection("orders")
pipeline := bson.A{
bson.D{{"$match", bson.D{{"amount", bson.D{{"$gt", 0}}}}}},
bson.D{{"$group", bson.D{
{"_id", "$status"},
{"count", bson.D{{"$sum", 1}}},
{"totalRevenue", bson.D{{"$sum", "$amount"}}},
}}},
bson.D{{"$project", bson.D{
{"status", "$_id"},
{"count", 1},
{"totalRevenue", bson.D{{"$round", bson.A{"$totalRevenue", 2}}}},
{"_id", 0},
}}},
bson.D{{"$sort", bson.D{{"totalRevenue", -1}}}},
}
cursor, _ := col.Aggregate(context.TODO(), pipeline)
var results []bson.M
cursor.All(context.TODO(), &results)
for _, r := range results { fmt.Println(r) }
}go run main.go