More commonly used are:
:$project - data can be reconstructed :$match - Enable query-like functionality :$limit - limit the number of returns $skip - Ibid. :$unwind - You can divide a document containing an array into several parts, such as your document has an array field A and 10 elements in A. After $unwind processing, 10 documents will be generated, which are only different from field A. :$group - Statistical operations, with a series of subcommands –$avg, $sum ... * sort - sort
Next, four functions will be implemented:
task 1: Statistics of the average age of Shanghai students
From this point of view, there are several steps to realize the function: 1. Find out the students in Shanghai; 2. Statistical average age (of course, you can also calculate the average value of all provinces before finding out Shanghai).
select province, avg(age) from student where province = 'Shanghai' group by province
java code:
/*Create $match, which acts as query*/ DBObject match = new BasicDBObject("$match", new BasicDBObject("province", "Shanghai")); /* Group operation*/ DBObject groupFields = new BasicDBObject("_id", "$province"); groupFields.put("AvgAge", new BasicDBObject("$avg", "$age")); DBObject group = new BasicDBObject("$group", groupFields); /* View Group results */ AggregationOutput output = connection.aggregate(match, group); // Execute aggregation command System.out.println(output.getCommandResult());
Output results:
"result" : [ { "_id" : "Shanghai" , "AvgAge" : 32.09375} ] , "ok" : 1.0
task2: Statistics of the average performance of each department in each province
First of all, it has more database document structure. subjects are array forms. They need to be grouped first and then counted.
The main processing steps are as follows:
1. Split the array with $unwind. 2. Rent subjects according to province and get the average score of each subject.
/* Create a $unwind operation for splitting groups*/ DBObject unwind = new BasicDBObject("$unwind", "$subjects"); /* Group operation*/ DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("subjname", "$subjects.name").append("province", "$province")); groupFields.put("AvgScore", new BasicDBObject("$avg", "$subjects.scores")); DBObject group = new BasicDBObject("$group", groupFields); /* View Group results */ AggregationOutput output = connection.aggregate(unwind, group); // Execute aggregation command System.out.println(output.getCommandResult());
Output results:
"result" : [ { "_id" : { "subjname" : "English" , "province" : "Hainan"} , "AvgScore" : 58.1} , { "_id" : { "subjname" : "Mathematics" , "province" : "Hainan"} , "AvgScore" : 60.485} , { "_id" : { "subjname" : "Chinese" , "province" : "Jiangxi"} , "AvgScore" : 55.538} , { "_id" : { "subjname" : "English" , "province" : "Shanghai"} , "AvgScore" : 57.65625} , { "_id" : { "subjname" : "Mathematics" , "province" : "Guangdong"} , "AvgScore" : 56.690} , { "_id" : { "subjname" : "Mathematics" , "province" : "Shanghai"} , "AvgScore" : 55.671875} , { "_id" : { "subjname" : "Chinese" , "province" : "Shanghai"} , "AvgScore" : 56.734375} , { "_id" : { "subjname" : "English" , "province" : "Yunnan"} , "AvgScore" : 55.7301 } , . . . . "ok" : 1.0
task3:
The results of subjects in the same province are counted together (i. E. expecting `province': `xxxx', avgscores: [{`xxx': xxxx},... Such a form)
One thing to do is, on the basis of the previous statistical results, first use project to knead the average score and achievement together, then push the average score of each subject into one group according to the provincial group, and then use group to group again.
Mongo m = new Mongo("localhost", 27017); DB db = m.getDB("test"); DBCollection coll = db.getCollection("student"); /* Create a $unwind operation for splitting groups*/ DBObject unwind = new BasicDBObject("$unwind", "$subjects"); /* Group operation*/ DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("subjname", "$subjects.name").append("province", "$province")); groupFields.put("AvgScore", new BasicDBObject("$avg", "$subjects.scores")); DBObject group = new BasicDBObject("$group", groupFields); /* Reshape Group Result*/ DBObject projectFields = new BasicDBObject(); projectFields.put("province", "$_id.province"); projectFields.put("subjinfo", new BasicDBObject("subjname","$_id.subjname").append("avgscore", "$AvgScore")); DBObject project = new BasicDBObject("$project", projectFields); /* push the results together*/ DBObject groupAgainFields = new BasicDBObject("_id", "$province"); groupAgainFields.put("avginfo", new BasicDBObject("$push", "$subjinfo")); DBObject reshapeGroup = new BasicDBObject("$group", groupAgainFields); /* View Group results */ AggregationOutput output = coll.aggregate(unwind, group, project, reshapeGroup); System.out.println(output.getCommandResult());
"result" : [ { "_id" : "Liaoning" , "avginfo" : [ { "subjname" : "Mathematics" , "avgscore" : 56.46666666666667} , { "subjname" : "English" , "avgscore" : 52.093333333333334} , { "subjname" : "Chinese" , "avgscore" : 50.53333333333333}]} , { "_id" : "Sichuan" , "avginfo" : [ { "subjname" : "Mathematics" , "avgscore" : 52.72727272727273} , { "subjname" : "English" , "avgscore" : 55.90909090909091} , { "subjname" : "Chinese" , "avgscore" : 57.59090909090909}]} , { "_id" : "Chongqing" , "avginfo" : [ { "subjname" : "Chinese" , "avgscore" : 56.077922077922075} , { "subjname" : "English" , "avgscore" : 54.84415584415584} , { "subjname" : "Mathematics" , "avgscore" : 55.33766233766234}]} , { "_id" : "Anhui" , "avginfo" : [ { "subjname" : "English" , "avgscore" : 55.458333333333336} , { "subjname" : "Mathematics" , "avgscore" : 54.47222222222222} , { "subjname" : "Chinese" , "avgscore" : 52.80555555555556}]} . . . ] , "ok" : 1.0}
task4:
DBCollection collection = MongoUtils.getCollection_Database( (String) ServletContextUtils.getSession().getAttribute( "game"), SysConst.TABLE_WAIGUA_RATIO); // query DBObject match = new BasicDBObject("$match", queryParam_n); // Using $project to assemble the data needed by the group DBObject fields = new BasicDBObject("name", 1); fields.put("count", 1); DBObject project = new BasicDBObject("$project", fields); DBObject groupFields = new BasicDBObject("_id", "$name"); groupFields.put("count", new BasicDBObject("$sum", "$count")); DBObject group = new BasicDBObject("$group", groupFields);// group DBObject limit = new BasicDBObject("$limit", Integer.parseInt(n)); DBObject sort = new BasicDBObject("$sort", new BasicDBObject( "count", -1)); AggregationOutput output = collection.aggregate(match, project, group, sort, limit); List<String> nameList = new ArrayList<String>(); for (DBObject obj : output.results()) { BasicDBObject obj2 = (BasicDBObject) obj; nameList.add(obj2.getString("_id")); }