focus on MongoDB sum of a field sum
New Basicdbobject ("$group", New Basicdbobject ("_id", "$number"). Append ("Total", New Basicdbobject ("$sum", "$" + "length ")));
New Basicdbobject ("_id", "$" +queryfield) is a fixed format
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
First, $match filters out where conditions basicdbobject[] array = {new Basicdbobject ("Starttimelong", New Basicdbobject ("$gte", begin
Time), 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 group data, including OptCode column, processtime column dbobject fields = new Basicdbobject ("OptCode", 1);
Interface Fields.put ("Processtime", 1);//Time-consuming fields.put ("Provincecode", 1);//Province Fields.put ("Channelsubcode", 1);//Channel
Fields.put ("Platformcode", 1);//platform DBObject project = new Basicdbobject ("$project", fields);
Use $group to 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);
/** *
sums a field *
@param collectionname
* @param where
* @param groupfields
* @return
* * Public Double sum (String collectionname,dbobject where,string sumfield) {
dbcollection col=mongodbfactory.getdb () . GetCollection (CollectionName);
DBObject match=new basicdbobject ("$match", where);
DBObject group=new basicdbobject ("$group", New Basicdbobject ("_id", null). Append ("Total", New Basicdbobject ("$sum", "$ "+sumfield)));
List<dbobject> list= (list<dbobject>) col.aggregate (match,group). Results ();
if (List.size () >0) {
return double.valueof (list.get (0). Get ("Total"). ToString ());
}
return 0 D;
}
Personal example: query consumption sum
Public long Getcostcount (string number, string type, int month) {
dbcollection DBC = GetCollection (Constant.mongodbna Me, constant.costdetails);
DBObject dbobject = new Basicdbobject ();
Dbobject.put ("number", number);
Dbobject.put ("type", type);
Dbobject.put ("Month", month);
Set the Where condition
dbobject match = new Basicdbobject ("$match", dbobject);
Set the grouping field
dbobject group=new basicdbobject ("$group", New Basicdbobject ("_id", "$number"). Append ("Total", new Basicdbobject ("$sum", "$" + "Length")));
List<dbobject> list= (list<dbobject>) dbc.aggregate (match,group). Results ();
if (list = null && list.size () >0) {
log.info (list.get (0). toString ());
Return long.valueof (List.get (0). Get ("Total"). ToString ());
else{
return 0l;
}
}