Answers
BasicDBObject group1 = new BasicDBObject();
BasicDBObject _id = new BasicDBObject();
_id.put("field1", "$field1");
_id.put("field2", "$field2");
group1.put("_id", _id);
BasicDBObject group2 = new BasicDBObject();
group2.put("_id", "$_id.field1");
group2.put("statCnt", new BasicDBObject("$sum", 1));
List<DBObject> aggParam = new ArrayList<>();
aggParam.add(new BasicDBObject("$match", match));
aggParam.add(new BasicDBObject("$group", group1));
aggParam.add(new BasicDBObject("$group", group2));
aggParam.add(new BasicDBObject("$sort", sort));
aggParam.add(new BasicDBObject("$limit", 10));
AggregationOutput output = collection.aggregate(aggParam);
这是我实际用到的代码,简单的说就是用了两次group,达到分组都对某个字段去重的目的
AKIKO
answered 9 years, 4 months ago