2016-10-14 116 views
1

这里是我的样本数据:两小时的MongoDB聚合

{ 
"_id": { 
    "$oid": "5654a8f0d487dd1434571a6e" 
}, 

"ValidationDate": { 
    "$date": "2015-11-24T13:06:19.363Z" 
}, 

"DataRaw": " WL 00100100012015-08-28 02:44:17+0000+ 16.81 8.879 1084.00", 

"ReadingsAreValid": true, 

"locationID": " WL 001", 

"Readings": { 

    "pH": { 
     "value": 8.879 
    }, 

    "SensoreDate": { 
     "value": { 
      "$date": "2015-08-28T02:44:17.000Z" 
     } 
    }, 

    "temperature": { 
     "value": 16.81 
    }, 

    "Conductivity": { 
     "value": 1084 
    } 
}, 
"HMAC":"ecb98d73fcb34ce2c5bbcc9c1265c8ca939f639d791a1de0f6275e2d0d71a801" 

}

我两个小时间隔试图组平均值并具有以下聚集查询。

Query = [{"$unwind":"$Readings"},      
     {'$group' : { "_id": { 
      "year": { "$year": "$Readings.SensoreDate.value" }, 
      "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" }, 
      "interval": { 
       "$subtract": [ 
        { "$hour": "$Readings.SensoreDate.value"}, 
        { "$mod": [{ "$hour": "$Readings.SensoreDate.value"},2]} 
       ] 
      } 
     }}, 
'AverageTemp' : { '$avg' : '$Readings.temperature.value'}, "AveragePH": {"$avg" : "$Readings.pH.value"}, "AverageConduc": {"$avg" : "$Readings.Conductivity.value"}} 
, {"$limit":10}] 

这给了我一个错误说 A pipeline stage specification object must contain exactly one field.,我已经做了所有的研究,但不能得到想要的结果。

回答

1

一些格式化之后,你现在聚集管道的样子:

Query = [ 
    { "$unwind": "$Readings" },      
    { 
     '$group' : { 
      "_id": { 
       "year": { "$year": "$Readings.SensoreDate.value" }, 
       "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" }, 
       "interval": { 
        "$subtract": [ 
         { "$hour": "$Readings.SensoreDate.value"}, 
         { 
          "$mod": [ 
           { "$hour": "$Readings.SensoreDate.value" }, 
           2 
          ] 
         } 
        ] 
       } 
      } 
     }, 
     'AverageTemp' : { '$avg' : '$Readings.temperature.value' }, 
     "AveragePH": { "$avg" : "$Readings.pH.value" }, 
     "AverageConduc": { "$avg" : "$Readings.Conductivity.value" } 
    }, 
    { "$limit": 10 } 
] 

与蒙戈抱怨

流水线级规范对象必须只包含一个领域。

,因为它没有识别出错位的领域

'AverageTemp' : { '$avg' : '$Readings.temperature.value' }, 
"AveragePH": { "$avg" : "$Readings.pH.value" }, 
"AverageConduc": { "$avg" : "$Readings.Conductivity.value" } 

正确的管道应该有$group流水线阶段内这些领域,所以工作管道如下:

Query = [ 
    { "$unwind": "$Readings" },      
    { 
     "$group" : { 
      "_id": { 
       "year": { "$year": "$Readings.SensoreDate.value" }, 
       "dayOfYear": { "$dayOfYear": "$Readings.SensoreDate.value" }, 
       "interval": { 
        "$subtract": [ 
         { "$hour": "$Readings.SensoreDate.value"}, 
         { 
          "$mod": [ 
           { "$hour": "$Readings.SensoreDate.value" }, 
           2 
          ] 
         } 
        ] 
       } 
      }, 
      "AverageTemp" : { "$avg" : "$Readings.temperature.value" }, 
      "AveragePH": { "$avg" : "$Readings.pH.value" }, 
      "AverageConduc": { "$avg" : "$Readings.Conductivity.value" } 
     }  
    }, 
    { "$limit": 10 } 
]