‘壹’ 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);