Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I aggregate nested documents?

I have a collection :

{
    _id : xxx,
    children : [
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        }
    ]
},

{
    _id : xxx,
    children : [
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        }
    ]
},

{
    _id : xxx,
    children : [
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        }
    ]
},

{
    _id : xxx,
    children : [
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        },
        {
            childrenOfChildren : [
                {
                    price : xxx
                },
                {
                    price : xxx
                },
                {
                    price : xxx
                }
            ]
        }
    ]
}

Every entry has an array named children. And Every entry in children has an array named childrenOfChildren. And every entry in childrenOfChildren has an attribute named price. I wanna get maximum value of price in this overall collection. How can I achieve this? Please help me!

like image 762
Jeffrey Kang Avatar asked Apr 16 '26 01:04

Jeffrey Kang


2 Answers

you can do this using $unwind and $group.

db.collection.aggregate([
   {
      $unwind:"$children"
   },
   {
      $unwind:"$children.childrenOfChildren"
   },
   {
      $group:{
         _id:null,
         maxPrice:{
            $max:"$children.childrenOfChildren.price"
         }
      }
   }
])

output:

{ "_id" : null, "maxPrice" : 110 }

try it online: mongoplayground.net/p/sBTclni0YSw

like image 192
felix Avatar answered Apr 18 '26 16:04

felix


you can get maximum price from overall collection by using aggregate query with $unwind and $group.

can try this query:

db.getCollection('collectionName').aggregate([
 {$unwind: "$children"},
 {$unwind: "$children.childrenOfChildren"},
 {$group:{_id: null, price:{$max: "$children.childrenOfChildren.price"}}}
])
like image 33
Shaishab Roy Avatar answered Apr 18 '26 16:04

Shaishab Roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!