准备数据 customer 集合,数据类型如下:(只是部分数据,数据量太多就不一一列出)
{ "_id" : ObjectId("594c78a1c5fea52a445b8cd2"), "probMac" : "141FBA789098", "devcMac" : "BC3AEA0E50CB", "inTime" : "20170612133513", "outTime" : "20170612133545", "rssi" : "14" }
{ "_id" : ObjectId("594c78a1c5fea52a445b8cd3"), "probMac" : "141FBA7890E8", "devcMac" : "38295A676DA3", "inTime" : "20170612133701", "outTime" : "20170612133716", "rssi" : "3" }
{ "_id" : ObjectId("594c78a1c5fea52a445b8cd4"), "probMac" : "141FBA789050", "devcMac" : "18D276C360FF", "inTime" : "20170612133746", "outTime" : "20170612133750", "rssi" : "8" }
{ "_id" : ObjectId("594c78a2c5fea52a445b8cd5"), "probMac" : "141FBA789050", "devcMac" : "E8BBA876D18A", "inTime" : "20170612134003", "outTime" : "20170612134012", "rssi" : "9" }
{ "_id" : ObjectId("594c78a2c5fea52a445b8cd6"), "probMac" : "141FBA789050", "devcMac" : "3CB6B75AFF3A", "inTime" : "20170612134133", "outTime" : "20170612134141", "rssi" : "11" }
{ "_id" : ObjectId("594c78a2c5fea52a445b8cd7"), "probMac" : "141FBA789050", "devcMac" : "D03742643B6D", "inTime" : "20170612134215", "outTime" : "20170612134247", "rssi" : "11" }
{ "_id" : ObjectId("594c78a2c5fea52a445b8cd8"), "probMac" : "141FBA789050", "devcMac" : "C09F054B6DB8", "inTime" : "20170612134217", "outTime" : "20170612134253", "rssi" : "27" }
1 通过mongoTemplate.group方法
public <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
public <T> GroupByResults<T> group(Criteria criteria, String inputCollectionName, GroupBy groupBy,
Class<T> entityClass)
一种可以带过滤条件一种全集合分组查询
使用第一种不带条件的查询
@RequestMapping("/test")
@ResponseBody
public Object test(){
//多个key组合分组,一个key分组掺入一个参数即可
GroupBy groupBy = GroupBy.key("devcMac","probMac").initialDocument("{total:0}")
.reduceFunction("function(doc, prev){prev.total+=1}");
GroupByResults<BasicDBObject> gbr = mongoTemplate.group("customer", groupBy, BasicDBObject.class);
return gbr.iterator();
}
返回结果:
[{"devcMac":"BC3AEA0E50CB","probMac":"141FBA789098","total":8.0},
{"devcMac":"38295A676DA3","probMac":"141FBA7890E8","total":1.0},
{"devcMac":"18D276C360FF","probMac":"141FBA789050","total":1.0},
{"devcMac":"E8BBA876D18A","probMac":"141FBA789050","total":2.0},
{"devcMac":"3CB6B75AFF3A","probMac":"141FBA789050","total":6.0},
{"devcMac":"D03742643B6D","probMac":"141FBA789050","total":1.0}]
如果想返回其他的字段可以更改reduce函数,和 initialDocument 参数
比如加上inTime值的数组内容
@RequestMapping("/groupByMutiKeyOther")
@ResponseBody
public Object groupByMutiKeyOther(String... key) {
// 多个key组合分组,一个key分组掺入一个参数即可
GroupBy groupBy = GroupBy.key("probMac","devcMac").initialDocument("{inTime:[],count:0}")
.reduceFunction("function(doc, prev){"
+ "prev.inTime.push(doc.inTime);"
+ "prev.count+=1}");
GroupByResults<BasicDBObject> gbr = mongoTemplate.group("customer",
groupBy, BasicDBObject.class);
return gbr.iterator();
}
返回结果:
[{"probMac":"141FBA789098","devcMac":"BC3AEA0E50CB","inTime":["20170612133513","20170612172022","20170612185417",
"20170613021023","20170628034820","20170628054026","20170628102731","20170628142909"],"count":8.0},
{"probMac":"141FBA7890E8","devcMac":"38295A676DA3","inTime":["20170612133701"],"count":1.0},
{"probMac":"141FBA789050","devcMac":"18D276C360FF","inTime":["20170612133746"],"count":1.0},
{"probMac":"141FBA789050","devcMac":"E8BBA876D18A","inTime":["20170612134003","20170617151506"],"count":2.0},
{"probMac":"141FBA789050","devcMac":"3CB6B75AFF3A","inTime":["20170612134133","20170616215301","20170619002323",
"20170625061702","20170625200712","20170627235117"],"count":6.0},
{"probMac":"141FBA789050","devcMac":"D03742643B6D","inTime":["20170612134215"],"count":1.0}]
2 mapreduce分组统计
@RequestMapping("/groupByMapReduce")
@ResponseBody
public Object groupByMapReduce(String key) {
String map = "function() { emit(this.probMac, {count:1});}";
String reduce = "function(key, values) {"
+ "var total = 0;"
+ "for(var i=0;i<values.length;i++){total += values[i].count;}"
+ "return {count:total};}";
MapReduceResults<BasicDBObject> mrr = mongoTemplate.mapReduce("customer", map, reduce, BasicDBObject.class);
return mrr.iterator();
}
返回结果内容:
[{"_id":"141FBA789010","value":{"count":505.0}},{"_id":"141FBA789050","value":{"count":18287.0}},{"_id":"141FBA789098","value":{"count":7489.0}},{"_id":"141FBA7890E8","value":{"count":8626.0}}]
3 通过 mongoTemplate.aggregate方法
@RequestMapping("/groupByAggregation")
@ResponseBody
public Object groupByAggregation(String key) {
AggregationResults<BasicDBObject> a = mongoTemplate.aggregate(
Aggregation.newAggregation(
Aggregation.group("probMac").count().as("count")), Customer.class, BasicDBObject.class);
return a.iterator();
}
返回结果:
[{"_id":"141FBA789010","count":505},{"_id":"141FBA789050","count":18291},{"_id":"141FBA7890E8","count":8644},{"_id":"141FBA789098","count":7493}]