2017-08-29 41 views
0

我正在学习MongoDB汇总管道,我试图从$unwind$group的输出中获得匹配计数。我能够看到$unwind$group的结果。但我不确定为什么我没有得到匹配的计数。请帮忙让个场大于25

下面是一个例子文件:

{ 
    "_id":ObjectId("599e9dbd8fbad926e712f902"), 
    "sample":"1", 
    "attribute":[ 
     { 
      "functionName":"1", 
      "percentage":31.6 
     } 
    ] 
} 

我尝试这样做:

db.docs3.aggregate({ 
    $unwind:'$attribute' 
}, 
{ 
    $group:{ 
     _id:{ 
      func:"$attribute.functionName", 
      percen:"$attribute.percentage" 
     } 
    } 
}) 

,并得到这样的输出:

{ "_id" : { "func" : "7", "percen" : 30 } } 
{ "_id" : { "func" : "5", "percen" : 23.1 } } 
{ "_id" : { "func" : "8", "percen" : 27.8 } } 
{ "_id" : { "func" : "6", "percen" : 32.1 } } 
{ "_id" : { "func" : "1", "percen" : 31.6 } } 
{ "_id" : { "func" : "2", "percen" : 35 } } 
{ "_id" : { "func" : "3", "percen" : 7.1 } } 
{ "_id" : { "func" : "4", "percen" : 31.6 } } 

我试过这个:

db.docs3.aggregate({ 
    $unwind:'$attribute' 
}, 
{ 
    $group:{ 
     _id:{ 
      func:"$attribute.functionName", 
      percen:"$attribute.percentage" 
     } 
    } 
}, 
{ 
    $match:{ 
     "attribute.percentage":{ 
      $gt:25 
     } 
    } 
}) 

我得到一个错误。

回答

0

我不知道你是否希望至少有一个属性的

  1. 文档计数与percentage > 25

或者

  • 属性有percentage > 25
  • 如果你有兴趣在1号,那么你不需要展开attribute阵列来应用你的比赛。下面将返回的文件包含至少一个属性的计数与percentage > 25

    db.getCollection('other').aggregate([ 
        { $match: { 'attribute.percentage': { $gt: 25 } } }, 
        { $group: { _id: null, count: { $sum: 1 } } } 
    ]) 
    

    如果你有兴趣,然后2号下面将返回属性的数量与percentage > 25

    db.getCollection('other').aggregate([ 
        { $unwind: '$attribute' }, 
        { $match: { 'attribute.percentage': { $gt: 25 } } }, 
        { $group: { _id: null, count: { $sum: 1 } } } 
    ]) 
    

    对于下列文件:

    { 
        "_id" : ObjectId("599e9dbd8fbad926e712f902"), 
        "sample" : "1", 
        "attribute" : [ 
         { 
          "functionName" : "1", 
          "percentage" : 31.6 
         } 
        ] 
    } 
    
    { 
        "_id" : ObjectId("59a54104e7e9cc2109863beb"), 
        "sample" : "1", 
        "attribute" : [ 
         { 
          "functionName" : "2", 
          "percentage" : 21.2 
         } 
        ] 
    } 
    
    { 
        "_id" : ObjectId("59a542c4e7e9cc2109863c45"), 
        "sample" : "1", 
        "attribute" : [ 
         { 
          "functionName" : "2", 
          "percentage" : 20.2 
         }, 
         { 
          "functionName" : "2", 
          "percentage" : 28.2 
         }, 
         { 
          "functionName" : "2", 
          "percentage" : 35.2 
         } 
        ] 
    } 
    

    第一命令返回count=2,第二命令返回count=3

    +0

    喜毛刺相同的字段名称,感谢您的答复。其实我试图获得> 25的百分比functionName的列表。 –

    +0

    @ ManiMuthu.K你可以请更新问题,包括一个示例文件,显示你想要的输出?这是表达您的要求的最明确的方式。 – glytching

    0

    用这笔钱标志,以获得当前值,并使用您在$小组赛阶段使用

    db.docs3.aggregate({ 
        $unwind:'$attribute' 
    }, 
    { 
        $group:{ 
         _id:{ 
          func:"$attribute.functionName", 
          percen:"$attribute.percentage" 
         } 
        } 
    }, 
    { 
        $match:{ 
         "$_id.percen":{ 
          $gt:25 
         } 
        } 
    })