Docs Menu

Docs HomeGo

Aggregation

On this page

  • Overview
  • Compare Operations
  • Limitations
  • Examples
  • Additional Information

In this guide, you can learn how to use aggregation operations in the MongoDB Go Driver.

Aggregation operations process data in your MongoDB collections based on your specifications in the aggregation pipeline. An aggregation pipeline consists of one or more stages. Each stage performs an operation based on its expression operators. After the driver executes the aggregation pipeline, it returns an aggregated result.

Aggregation operations operate similarly to a car factory. Car factories have an assembly line. The assembly lines have assembly stations with specialized tools to peform a specific task. To build a car, you send raw parts to the factory. Then, the assembly line transforms and assembles the parts into a car.

The assembly line resembles the aggregation pipeline, the assembly stations in the assembly line resemble the aggregation stages, the specialized tools represent the expression operators, and the finished product resembles the aggregated result.

The following table lists the tasks you can perform with find and aggregation operations.

Find Operations
Aggregation Operations
Select what documents to return
Select which fields to return
Sort the results
Limit the results
Count the results
Select what documents to return
Select which fields to return
Sort the results
Limit the results
Count the results
Rename fields
Calculate fields
Summarize data
Group values

Aggregation operations have limitations. When performing agregation operations, keep the following in mind:

  • Returned documents must not violate the BSON document size limit of 16 megabytes.

  • Pipeline stages have a memory limit of 100 megabytes by default. If required, you may exceed this limit by using the allowDiskUse method.

  • The $graphLookup stage has a strict memory limit of 100 megabytes and ignores allowDiskUse.

To run the examples in this section, load the sample data into the tea.menu collection with the following snippet:

coll := client.Database("tea").Collection("menu")
docs := []interface{}{
bson.D{{"type", "Masala"}, {"category", "black"}, {"toppings", bson.A{"ginger", "pumpkin spice", "cinnomon"}}, {"price", 6.75}},
bson.D{{"type", "Gyokuro"}, {"category", "green"}, {"toppings", bson.A{"berries", "milk foam"}}, {"price", 5.65}},
bson.D{{"type", "English Breakfast"}, {"category", "black"}, {"toppings", bson.A{"whipped cream", "honey"}}, {"price", 5.75}},
bson.D{{"type", "Sencha"}, {"category", "green"}, {"toppings", bson.A{"lemon", "whipped cream"}}, {"price", 5.15}},
bson.D{{"type", "Assam"}, {"category", "black"}, {"toppings", bson.A{"milk foam", "honey", "berries"}}, {"price", 5.65}},
bson.D{{"type", "Matcha"}, {"category", "green"}, {"toppings", bson.A{"whipped cream", "honey"}}, {"price", 6.45}},
bson.D{{"type", "Earl Grey"}, {"category", "black"}, {"toppings", bson.A{"milk foam", "pumpkin spice"}}, {"price", 6.15}},
bson.D{{"type", "Hojicha"}, {"category", "green"}, {"toppings", bson.A{"lemon", "ginger", "milk foam"}}, {"price", 5.55}},
}
result, err := coll.InsertMany(context.TODO(), docs)
if err != nil {
panic(err)
}

Each document represents a tea on the menu of a shop. It conatains information about the tea type, the toppings you can add, and its price.

The following example calculates and displays the average rating and amount of ratings for each tea.

The aggregation pipleline uses the $group stage to group the documents by the category field, calculates the average using the $avg expression operator, and counts the number of documents using the $sum expression operator.

// create the stage
groupStage := bson.D{
{"$group", bson.D{
{"_id", "$category"},
{"average_price", bson.D{
{"$avg", "$price"},
}},
{"type_total", bson.D{
{"$sum", 1},
}},
}}}
// pass the stage into a pipeline
// pass the pipeline as the second paramter in the Aggregate() method
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
if err != nil {
panic(err)
}
// display the results
var results []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
fmt.Printf("Average price of %v tea: $%v \n", result["_id"], result["average_price"])
fmt.Printf("Amount of %v tea: %v \n\n", result["_id"], result["type_total"])
}

The following example matches documents where you can get milk foam as a topping and lists the two cheapest options.

The aggregation pipleline contains the following stages:

  • $match stage to match documents where its toppings contain "milk foam"

  • $unset stage to omit the _id and category fields

  • $sort stage to sort the price and toppings in ascending order

  • $limit stage to show the first two documents

// create the stages
matchStage := bson.D{{"$match", bson.D{{"toppings", "milk foam"}}}}
unsetStage := bson.D{{"$unset", bson.A{"_id", "category"}}}
sortStage := bson.D{{"$sort", bson.D{
{"price", 1},
{"toppings", 1}},
}}
limitStage := bson.D{{"$limit", 2}}
// pass the stage into a pipeline
// pass the pipeline as the second paramter in the Aggregate() method
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, unsetStage, sortStage, limitStage})
if err != nil {
panic(err)
}
// display the results
var results []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
fmt.Printf("Tea: %v \nToppings: %v \nPrice: $%v \n\n", result["type"], result["toppings"], result["price"])
}

To learn more about the terms mentioned, see the following guides:

To view more aggregation examples, see the following guides:

To learn more about the Aggregate() method and its behavior, see Retrieve Data.

To learn more about any of the methods or types discussed in this guide, see the following API Documentation:

←  Compound OperationsIndexes →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.