2016-12-28 171 views
2

我有一個集合:如何彙總嵌套文檔?

{ 
    _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 
       } 
      ] 
     } 
    ] 
} 

每個條目都有一個名爲孩子的數組。並且每個兒童項目都有一個名爲childrenOfChildren的數組。並且childrenOfChildren中的每個條目都有一個名爲的屬性,價格爲。我想在這個整體收藏中獲得最大的價格價值。我怎樣才能做到這一點?請幫幫我!

+0

可以嘗試使用'的[?我怎樣才能在嵌套文件最大值(aggregate'查詢 –

+0

可能的複製http://stackoverflow.com/questions/41327672 /如何-可以-I-GET-MAX-值在嵌套的文檔) – styvane

回答

2

,你可以做到這一點使用$放鬆和$組。

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

輸出:

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

您可以通過使用aggregate$unwind$group查詢從整體收集中獲得最高價格。

可以嘗試此查詢:

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