How can I aggregate nested documents in MongoDB?

To aggregate nested documents in MongoDB, you can use the aggregation pipeline with operators like $unwind and $group. The $unwind operator flattens array fields, allowing you to process individual elements within nested structures.

Syntax

db.collection.aggregate([
    { $unwind: "$arrayField" },
    { $unwind: "$arrayField.nestedArray" },
    { $group: { _id: null, result: { $operation: "$arrayField.nestedArray.field" } } }
]);

Sample Data

db.aggregateDemo.insertMany([
    {
        "ProductInformation": [
            {
                "Product1": [
                    { "Amount": 50 },
                    { "Amount": 90 },
                    { "Amount": 30 }
                ]
            },
            {
                "Product1": [
                    { "Amount": 200 },
                    { "Amount": 30 },
                    { "Amount": 40 }
                ]
            },
            {
                "Product1": [
                    { "Amount": 150 },
                    { "Amount": 190 },
                    { "Amount": 198 }
                ]
            }
        ]
    },
    {
        "ProductInformation": [
            {
                "Product1": [
                    { "Amount": 100 },
                    { "Amount": 1002 },
                    { "Amount": 78 }
                ]
            },
            {
                "Product1": [
                    { "Amount": 75 },
                    { "Amount": 400 },
                    { "Amount": 600 }
                ]
            },
            {
                "Product1": [
                    { "Amount": 700 },
                    { "Amount": 500 },
                    { "Amount": 600 }
                ]
            }
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("..."),
        ObjectId("...")
    ]
}

Example: Find Maximum Amount

To aggregate nested documents and find the maximum amount across all Product1 arrays ?

db.aggregateDemo.aggregate([
    {
        $unwind: "$ProductInformation"
    },
    {
        $unwind: "$ProductInformation.Product1"
    },
    {
        $group: {
            _id: null,
            MaximumAmount: {
                $max: "$ProductInformation.Product1.Amount"
            }
        }
    }
]);
{ "_id": null, "MaximumAmount": 1002 }

How It Works

  • First $unwind: Flattens the ProductInformation array, creating separate documents for each array element
  • Second $unwind: Flattens the nested Product1 arrays within each ProductInformation element
  • $group: Groups all unwound documents and applies the $max operator to find the highest Amount value

Conclusion

Use $unwind to flatten nested arrays and $group with aggregation operators to process nested document data. Multiple $unwind stages handle deeply nested structures by flattening each array level sequentially.

Updated on: 2026-03-15T01:47:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements