Group BY query do not use the Java drive with the group by, to use the 2.2 version of the aggregate aggregation framework, after the test speed of one times faster
Reference
Official website: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
Example: http://www.yeetrack.com/?p=649
Before viewing the example, please look at the 2 reference documents, especially the official website
Example
[Java]View Plaincopy
- First, $match filters out where conditions
- Basicdbobject[] Array = {
- New Basicdbobject ("Starttimelong", new Basicdbobject ("$gte", BeginTime)),
- New Basicdbobject ("Starttimelong", new Basicdbobject ("$lt", EndTime)};
- Basicdbobject cond = new Basicdbobject ();
- Cond.put ("$and", array);
- DBObject match = new Basicdbobject ("$match", cond);
- Use $project to assemble the data required by the group, including OptCode columns, processtime columns
- DBObject fields = new Basicdbobject ("OptCode", 1); //Interface
- Fields.put ("Processtime", 1); Time consuming
- Fields.put ("Provincecode", 1); Provinces
- Fields.put ("Channelsubcode", 1); Channel
- Fields.put ("Platformcode", 1); Platform
- DBObject project = new Basicdbobject ("$project", fields);
- Grouping with $group
- DBObject _group = new Basicdbobject ("Provincecode", "$provinceCode");
- _group.put ("Channelsubcode", "$channelSubCode");
- _group.put ("Platformcode", "$platFormCode");
- _group.put ("OptCode", "$optCode");
- DBObject groupfields = new Basicdbobject ("_id", _group);
- Total
- Groupfields.put ("Count", new Basicdbobject ("$sum", 1));
- //Sum
- Groupfields.put ("processtime_sum", new Basicdbobject ("$sum","$processTime"));
- DBObject Group = new Basicdbobject ("$group", groupfields);
- Aggregationoutput output = Mongodbutil.getloginfocollection (). Aggregate (match, project, group);
Advanced Operations for MongoDB (aggregation framework)