Prepare the data customer collection with the following data types: (only part of the data, the amount of data is too much to list)
{"_id": ObjectId ("594C78A1C5FEA52A445B8CD2"), "Probmac": "141fba789098", "Devcmac": "BC3AEA0E50CB", "InTime": "201706 12133513 "," Outtime ":" 20170612133545 "," Rssi ":" + "} {" _id ": ObjectId (" 594C78A1C5FEA52A445B8CD3 ")," Probmac ":" 141F Ba7890e8 "," 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 ":" 201706121 34133 "," Outtime ":" 20170612134141 "," Rssi ":" One "} {" _id ": ObjectId (" 594c78a2c5fea52a445b8cd7 ")," Probmac ":" 141fba7 89050 "," Devcmac ":" d03742643b6d "," InTIME ":" 20170612134215 "," Outtime ":" 20170612134247 "," Rssi ":" One "} {" _id ": ObjectId (" 594c78a2c5fea52a445b8cd8 ")," PR Obmac ":" 141fba789050 "," Devcmac ":" C09f054b6db8 "," InTime ":" 20170612134217 "," Outtime ":" 20170612134253 "," Rssi ":" 27 "}
1 through the Mongotemplate.group method
Public <T> groupbyresults<t> Group (String inputcollectionname, GroupBy GroupBy, class<t> entityclass )
Public <T> groupbyresults<t> Group (criteria, String inputcollectionname, GroupBy GroupBy,
Class<t> Entityclass)
A kind of whole set group query with filter condition
Use the first non-conditional query
@RequestMapping ("/test")
@ResponseBody public
Object test () {
//Multiple key combination grouping, a key grouping with one parameter can be
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 ();
}
return Result:
[{"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}]
If you want to return other fields, you can change the reduce function, and the initialdocument parameter
For example, adding an array of Intime values
@RequestMapping ("/groupbymutikeyother")
@ResponseBody public
Object groupbymutikeyother (String ... key) {
//Multiple key combination grouping, a key grouping with one parameter can
be 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 ();
}
return Result:
[{"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 Grouping statistics
@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 ();
}
Return result content:
[{"_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 through the Mongotemplate.aggregate method
@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 ();
}
return Result:
[{"_id": "141fba789010", "Count": 505},{"_id": "141fba789050", "Count": 18291},{"_id": "141fba7890e8", "Count": 8644},{ "_id": "141fba789098", "Count": 7493}]