DEV Community

Cover image for What is Aggregation Pipeline in MongoDB?
Prantik Ghosh
Prantik Ghosh

Posted on

What is Aggregation Pipeline in MongoDB?

Aggregation in MongoDB is referred as the process of performing different types of operations on multiple documents within a collection. This is done by using pipelines, which is made up of different stages. One stage output is the input for another/next stage.

Types of Aggregation pipelines:

  1. $match: This is a fundamental pipeline stage used within the Aggregation to filter documents. It functions similarly to the standard find() query but is designed to work as a step in a multi-stage data processing pipeline.

Example:

const channel = await User.aggregate([
        {
            $match: {
                username: username?.toLowerCase()
            }
        },
Enter fullscreen mode Exit fullscreen mode
  1. $lookup: This is an aggregation stage used to perform a left outer join between two collections in the same database. It allows us to merge data from a foreign collection into documents from a local collection, enriching them with related information. The $lookup passes the reshaped stage to next stage.

Example:

        {
            $lookup: {
                from: 'subscriptions',
                localField: '_id',
                foreignField: 'channel',
                as: 'subscribers'
            }
        },
        {
            $lookup: {
                from: 'subscriptions',
                localField: '_id',
                foreignField: 'subscriber',
                as: 'subscribedTo'
            }
        },
Enter fullscreen mode Exit fullscreen mode
  1. $addFields: This is an aggregation pipeline stage that adds new fields to documents or overwrites existing ones. It is particularly useful when we want to include all existing fields in the output while only modifying or adding a few specific ones. Note: As per the docs, we can also use $set, as $set is an alias of $addFields.

Example:

        {
            $addFields: {
                subscribersCount: {
                    $size: '$subscribers'
                },
                channelsSubscribedToCount: {
                    $size: '$subscribedTo'
                },
                isSubscribed: {
                    $cond: {
                        if: {$in: [req.user?._id, '$subscribers.subscribers']},
                        then: true,
                        else: false
                    }
                }
            }
        }
Enter fullscreen mode Exit fullscreen mode

In the above example, $cond is a ternary aggregation operator that allows us to implement if-then-else logic within aggregation pipelines, projections, and updates. It evaluates a boolean expression and returns one of two specified values based on the result.

  1. $project: This is an aggregation pipeline stage that passes only specified fields to the next stage. It is used to:
  2. include or exclude fields: 1(true) to include a field and 0(false) to exclude.
  3. create new fields: add computed fields or rename existing ones by assigning an expression to a new field name.
  4. reshape data: it can extract sub-fields from nested documents or create new array fields.

Example:

{
            $project: {
                fullName: 1,
                username: 1,
                subscribersCount: 1,
                channelsSubscribedToCount: 1,
                isSubscribed: 1,
                avatar: 1,
                coverImage: 1,
                email: 1,
            }
        }
    ]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)