3

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!

s7vr
  • 73,656
  • 11
  • 106
  • 127
Jeffrey Kang
  • 77
  • 1
  • 8

2 Answers2

3

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

felix
  • 9,007
  • 7
  • 41
  • 62
1

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"}}}
])
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68