How MongoDB Aggregation Pipelines Works ?

If you are interested to learn about the Update Operator in MongoDB

Aggregation Pipelines

Aggregation operations allow you to group, sort, perform calculations, analyze data, and much more.

Aggregation pipelines can have one or more “stages”. The order of these stages are important. Each stage acts upon the results of the previous stage.

Example

db.posts.aggregate([
  // Stage 1: Only find documents that have more than 1 like
  {
    $match: { likes: { $gt: 1 } }
  },
  // Stage 2: Group documents by category and sum each categories likes
  {
    $group: { _id: "$category", totalLikes: { $sum: "$likes" } }
  }
])

Sample Data

To demonstrate the use of stages in a aggregation pipeline, we will load sample data into our database. From the MongoDB Atlas dashboard, go to Databases. Click the ellipsis and select “Load Sample Dataset”. This will load several sample datasets into your database.

In the next sections we will explore several aggregation pipeline stages in more detail using this sample data.

Aggregation $group

This aggregation stage groups documents by the unique _id expression provided.

Don’t confuse this _id expression with the _id ObjectId provided to each document.

Example

In this example, we are using the “sample_airbnb” database loaded from our sample data in the Intro to Aggregations section.

db.listingsAndReviews.aggregate(
    [ { $group : { _id : "$property_type" } } ]
)

Aggregation $limit

This aggregation stage limits the number of documents passed to the next stage.

Example

In this example, we are using the “sample_mflix” database loaded from our sample data in the Intro to Aggregations section.

db.movies.aggregate([ { $limit: 1 } ])

Aggregation $project

This aggregation stage passes only the specified fields along to the next aggregation stage.

This is the same projection that is used with the find() method.

Example

In this example, we are using the “sample_restaurants” database loaded from our sample data in the Intro to Aggregations section.

db.restaurants.aggregate([
  {
    $project: {
      "name": 1,
      "cuisine": 1,
      "address": 1
    }
  },
  {
    $limit: 5
  }
])

Aggregation $sort

This aggregation stage groups sorts all documents in the specified sort order. Remember that the order of your stages matters. Each stage only acts upon the documents that previous stages provide.

Example

In this example, we are using the “sample_airbnb” database loaded from our sample data in the Intro to Aggregations section.

db.listingsAndReviews.aggregate([ 
  { 
    $sort: { "accommodates": -1 } 
  },
  {
    $project: {
      "name": 1,
      "accommodates": 1
    }
  },
  {
    $limit: 5
  }
])

Aggregation $match

This aggregation stage behaves like a find. It will filter documents that match the query provided. Using $match early in the pipeline can improve performance since it limits the number of documents the next stages must process.

Example

In this example, we are using the “sample_airbnb” database loaded from our sample data in the Intro to Aggregations section.

db.listingsAndReviews.aggregate([ 
  { $match : { property_type : "House" } },
  { $limit: 2 },
  { $project: {
    "name": 1,
    "bedrooms": 1,
    "price": 1
  }}
])

Aggregation $addFields

This aggregation stage adds new fields to documents.

Example

In this example, we are using the “sample_restaurants” database loaded from our sample data in the Intro to Aggregations section.

db.restaurants.aggregate([
  {
    $addFields: {
      avgGrade: { $avg: "$grades.score" }
    }
  },
  {
    $project: {
      "name": 1,
      "avgGrade": 1
    }
  },
  {
    $limit: 5
  }
]}

Aggregation $count

This aggregation stage counts the total amount of documents passed from the previous stage.

Example

In this example, we are using the “sample_restaurants” database loaded from our sample data in the Intro to Aggregations section.

db.restaurants.aggregate([
  {
    $match: { "cuisine": "Chinese" }
  },
  {
    $count: "totalChinese"
  }
])

Aggregation $lookup

This aggregation stage performs a left outer join to a collection in the same database.

There are four required fields:

  • from: The collection to use for lookup in the same database
  • localField: The field in the primary collection that can be used as a unique identifier in the from collection.
  • foreignField: The field in the from collection that can be used as a unique identifier in the primary collection.
  • as: The name of the new field that will contain the matching documents from the from collection.

Example

In this example, we are using the “sample_mflix” database loaded from our sample data in the Intro to Aggregations section.

db.comments.aggregate([
  {
    $lookup: {
      from: "movies",
      localField: "movie_id",
      foreignField: "_id",
      as: "movie_details",
    },
  },
  {
    $limit: 1
  }
])

Aggregation $out

This aggregation stage writes the returned documents from the aggregation pipeline to a collection.

The $out stage must be the last stage of the aggregation pipeline.

Example

In this example, we are using the “sample_airbnb” database loaded from our sample data in the Intro to Aggregations section.

db.listingsAndReviews.aggregate([
  {
    $group: {
      _id: "$property_type",
      properties: {
        $push: {
          name: "$name",
          accommodates: "$accommodates",
          price: "$price",
        },
      },
    },
  },
  { $out: "properties_by_type" },
])
How MongoDB Aggregation Pipelines Works ?
Show Buttons
Hide Buttons