mongo java --group by; max


Date lastExcTime=new Date();app

long startIndex=0;spa

 int pageSize=10;ip

List<DBObject> pipeline = new ArrayList<DBObject>();get

BasicDBObject group = new BasicDBObject();it

BasicDBObject val = new BasicDBObject();pip

val.append("_id","$userId");io

val.append("createTime",new BasicDBObject("$max","$createTime"));ast

group.append("$group", val);class

pipeline.add(group);List

BasicDBObject match = new BasicDBObject();

BasicDBObject matchVal = new BasicDBObject();

matchVal.append("createTime",new BasicDBObject("$gte",lastExcTime));

match.append("$match", matchVal);

pipeline.add(match);

BasicDBObject project = new BasicDBObject();

BasicDBObject projectVal = new BasicDBObject();

projectVal.append("createTime",1);

projectVal.append("userId",1);

projectVal.append("_id",-1);

project.append("$project", projectVal);

pipeline.add(project);

pipeline.add(new BasicDBObject("$skip",startIndex));

pipeline.add(new BasicDBObject("$limit",pageSize));

AggregationOutput aggregate = mongoTemplate.getCollection("homeLog").aggregate(pipeline);

HomeLog log=new HomeLog();

List<HomeLog> list=new ArrayList<HomeLog>();

JSONArray bean=JSONArray.parseArray(aggregate.getCommandResult().getString("result"));

for (int i = 0; i < bean.size(); i++) {

log = JSONObject.toJavaObject(bean.getJSONObject(i),HomeLog.class);

list.add(log);

}

相關文章
相關標籤/搜索