2016-12-28 172 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"}}} 
])