Nov 10, 2019· 9 mins to read

MongoDB Aggregation Example - Write Complex queries using MongoDB


MongoDB Aggregation Example - Write Complex queries using MongoDB

When your application starts to grow, you need to process data from various tables/collections to make it consumable by API or end-user. MongoDB aggregation example will show you how you can write complex queries in MongoDB using aggregation.

This article will explain what aggregation is in MongoDB and how to build MongoDB aggregation pipelines. Learn MongoDB Aggregation with a real-world example.

MongoDB Aggregation Example

When we start using MongoDB in the early stage of application. We only need to write simple CRUD-based queries that fetch the data from MongoDB.

But, when an application gets more complex, we need to perform several operations on the data before sending it to the API or end-user.

For example, consider that you are building an Analytical Dashboard where you need to show all the tasks for each user. Here, the server should send all tasks for each user in an array.

Can you guess how to achieve this in our data?.. So here comes the role of MongoDB Aggregation

Moreover, This is one of the use-cases that you will face while building an application. A situation like this will often come when the application handles millions of data. So, we will explore how we can solve this using MongoDB Aggregation Example.

MongoDB Aggregation Pipeline

Firstly, MongoDB aggregation is a pipeline that processes the data on each pipeline stage. Each stage returns the output, which turns into the input for the next pipeline in the stage.

MongoDB aggregation example

Here, Data is passed in each pipeline which filters, group and sort the data and returns the result.

Let’s see some of the Aggregation Operators that are widely used in real world applications.

Here’s an example of how we are going to build mongodb aggregation pipeline,

pipeline = [
  { $match : { … },
  { $group : { … },
  { $sort : { … },
  ...
]
db.collectionName.aggregate(pipeline, options)

Before, getting further into aggregation. I recommend you to import the sample dataset to practice along with this tutorial.

Once you download the data, you can import the dataset to MongoDB using the mongoimport command.

import - monogdb aggregation example

mongoimport --db aggrsample --collection test --file sample.json --jsonArray

$match Operator

match operator is similar to find() operator in MongoDB except that match works with aggregation. Likewise, the match pipeline operator matches all the documents that satisfy the condition.

On the above dataset, let’s try to match all the documents which have MA as a state.

db.test
  .aggregate([
    {
      $match: {
        state: "MA",
      },
    },
  ])
  .pretty();

As a result, it will find all the documents with MA as a state. output will be like,

match query MongoDB aggregation example

$group operator

As the name suggests, it groups the documents based on the particular field. It can be id or any other field.

On the top of the match command, let’s group the documents by city.

db.test.aggregate([
  {
    $match: {
      state: "MA",
    },
  },
  {
    $group: {
      _id: "$city",
    },
  },
]);

Further, you can see the result,

only group mongodb aggregation example

but wait, we want to retrieve all the fields in the document. Why does it only return the grouping field?.

Well, there is a reason for it. Once we group the document with _id field( _id field can contain any grouping field).

Then, we need to provide accumulator expression to retrieve the result of the grouping pipeline. popular accumulator expressions are

  • $first - this expression will return the first document of grouping result
  • $push - it will push all the documents into an array based on grouping result
  • $max - returns the highest value from the grouping documents.
  • $sum - returns the sum(numerial value) of the grouping documents.

Here, we will see how to use push expression along with group

db.test
  .aggregate([
    {
      $match: {
        state: "MA",
      },
    },
    {
      $group: {
        _id: "$city",
        data: {
          $push: "$$ROOT",
        },
      },
    },
  ])
  .pretty();

So, it will return documents like,

group push mongodb aggregation example

$project operator

Sometimes, you may not need all the fields in the document. You can only retrieve specific fields in the document using project operator.

db.test
  .aggregate([
    {
      $match: {
        state: "MA",
      },
    },
    {
      $group: {
        _id: "$city",
        data: {
          $push: "$$ROOT",
        },
      },
    },
    {
      $project: {
        _id: 0,
        "data.loc": 1,
      },
    },
  ])
  .pretty();

project command mongodb aggregation example

moreover, you can specify 0 or 1 to retrieve the specific field. It only retrieves _id by default. But, you can specify _id as 0 to avoid that.

$sort operator

Above all, the sort operator sorts the document in either ascending or descending order.

db.test.aggregate([
  {
    $match: {
      state: "MA",
    },
  },
  {
    $sort: {
      pop: 1,
    },
  },
]);

Mainly, it sorts the documents based on field pop with ascending order(1 if it is ascending, -1 if it is descending).

sort command mongodb aggregation example

$limit operator

Limit operator limits the number of documents retrieved from the database.

db.test.aggregate([
  {
    $match: {
      state: "MA",
    },
  },
  {
    $sort: {
      pop: 1,
    },
  },
  {
    $limit: 5,
  },
]);

So, the above query will return only five documents from the database.

limit command mongodb aggreation example

$addFields operator

Sometimes you need to create a custom field based on the aggregated data. you can achieve this using $addField operator.

db.test.aggregate([
  {
    $match: {
      state: "MA",
    },
  },
  {
    $addFields: {
      stateAlias: "MAS",
    },
  },
]);

As a result, it will return documents such as

add field

$lookup operator

After that, lookup is one of the popular aggregation operators in mongodb. if you are from SQL background. you can relate this with JOIN Query in RDBMS.

db.universities
  .aggregate([
    { $match: { name: "USAL" } },
    { $project: { _id: 0, name: 1 } },
    {
      $lookup: {
        from: "courses",
        localField: "name",
        foreignField: "university",
        as: "courses",
      },
    },
  ])
  .pretty();
  • from - it takes the collection that it wants to perform the join.
  • localField - it specifies the field from the input document. Here, it takes the field name from the universities collection.
  • foreignField - it specifies the field from the collection that it performs the join. Here, it is a university field from courses collection.
  • as - it specifies the alias for the field name.

Summary

To sum up, these are all the most common and popular aggregation operators in MongoDB. We will see in-depth concepts of MongoDB aggregation example in upcoming articles.

Recent Articles

TypeScript Basics – The Definitive Guide

Building a Production – Ready Node.js App with TypeScript and Docker

PM2 for Node.js developers

Copyright © Cloudnweb. All rights reserved.