mongo聚合和mapreduce例子
聚合语句-比较集合内两字段大小
db.test.aggregate([
{$match:{"offlineTime":{$gt:ISODate("2016-09-13T01:54:55Z"), "$lt":ISODate("2016-09-13T10:05:55Z")},"logType":"off"}},
{$project:{updateTime:1,offlineTime:1,offTimeInc:{$add:['$offlineTime',1800000]},mac:1}},
{$project:{updateTime:1,offlineTime:1,offTimeInc:1,mac:1,eq:{$cond:[{$gt:['$updateTime','$offTimeInc']}, 1, 0]}}},
{$match:{eq:1}}
])
聚合条数语句
db.tableName.aggregate([ {$match:{"offlineTime":{$gt:ISODate("2016-09-13T01:54:55Z"), "$lt":ISODate("2016-09-13T10:05:55Z")},"logType":"off"}}, {$project:{updateTime:1,offlineTime:1,offTimeInc:{$add:['$offlineTime',1800000]},mac:1}}, {$project:{updateTime:1,offlineTime:1,offTimeInc:1,mac:1,eq:{$cond:[{$gt:['$updateTime','$offTimeInc']}, 1, 0]}}}, {$match:{eq:1}}, {$group:{_id:null, total:{$sum:1}}} ])
简单聚合语句
db.test.aggregate( [ { $match : { start:{$gte: 1477411200000, $lte: 1477497600000}, buildingId: 49, intfType:"uplink", utcCode:8} }, { $group: { _id: {buildingId:"$buildingId", mac:"$mac"}, rxtxByteTotal: { $sum: "$rxTxBytes" } } } ] );
简单的mapreduce
var map = function(){ emit({"buildingId" : this.buildingId , "mac" : this.mac , "utcCode" : this.utcCode}, {"wifiUpDown" : this.wifiUpDown , "activeTime" : this.activeTime }); } var reduce = function(key, values){ var wifi = 0; var activeTime = 0; values.forEach(function(val){ wifi += val.wifiUpDown; activeTime += val.activeTime; }) return {"wifi": wifi, "activeTime": activeTime}; } db.test.mapReduce(map,reduce,{out:"mr2"})
上述为shell中直接执行的脚本。
使用spring-data-mongodb的版本:
String inCollectionName = "test"; String mapFunction = "function(){" + "emit({buildingId : this.buildingId , " + "mac : this.mac , utcCode : this.utcCode}, " + " {wifiUpDown : this.wifiUpDown , " + "activeTime : this.activeTime });}"; String reduceFunction = "function(key, values){ " + "var wifi = 0;" + "var activeTime = 0;" + "values.forEach(function(val){ " + "wifi += val.wifiUpDown;" + "activeTime += val.activeTime;" + " });" + " return {wifi: wifi, activeTime: activeTime};" + "}"; List<Test> result = new ArrayList<>(); mongo.mapReduce(inCollectionName, mapFunction, reduceFunction, Test.class).forEach( data->result.add(data) ); result.forEach(data->System.out.println(JSON.toJSONString(data)));
带project的聚合例子
db.test.aggregate( [ { $match : { day:20161128} }, { $group: { _id: {buildingId:"$buildingId", intfType:"$intfType", aggregatedTime:"$aggregatedTime", day:"$day"} , rxTxBytes: { $sum: "$rxTxBytes" } , rxBytes: {$sum: "$rxBytes"} , txBytes: {$sum: "$txBytes"} , rxPkts: {$sum: "$rxPkts"} , txPkts: {$sum: "$txPkts"} } }, { $project:{"_id":0, buildingId: "$_id.buildingId", intfType:"$_id.intfType", day:"$_id.day", rxBytes:1, rxPkts:1, txBytes:1, txPkts:1, aggregatedTime:"$_id.aggregatedTime", rxTxBytes:1} } ] );