『壹』 java mongodb多條件分組
//首先利$match篩選出where條件此處看你的查詢范圍條件了
BasicDBObject[]array={newBasicDBObject("startTimeLong",newBasicDBObject("$gte",beginTime)),
newBasicDBObject("startTimeLong",newBasicDBObject("$lt",endTime))};
BasicDBObjectcond=newBasicDBObject();
cond.put("$and",array);
DBObjectmatch=newBasicDBObject("$match",cond);
//利用$project拼裝group需要的數據,包含name列、age列
DBObjectfields=newBasicDBObject("name",1);
fields.put("age",1);
DBObjectproject=newBasicDBObject("$project",fields);
//利用$group進行分組
DBObject_group=newBasicDBObject("name","$name");
_group.put("age","$age");
DBObjectgroupFields=newBasicDBObject("_id",_group);
//總數
groupFields.put("count",newBasicDBObject("$sum",1));
DBObjectgroup=newBasicDBObject("$group",groupFields);
AggregationOutputoutput=MongodbUtil.getLogInfoCollection().aggregate(match,project,group);