MongoDb3.2 group by 实现方式 java版
最近在搞mongo 一个group by搞了好久, 终于搞出来了 .贴出来帮助下需要的小伙伴.
/** * * 多条件查询例子 */ public void moreQueryExmple() { List<Bson> list = new ArrayList<Bson>(); Bson bson1 = Filters.eq("trans_type", "免费资源查询"); Bson bson2 = Filters.eq("ret_code", "0"); list.add(bson1); list.add(bson2); Bson bson = Filters.and(list); FindIterable<Document> ll = MongoUtil.instance.getCollection("lghtest", "detailList").find(bson); } /*** * * 多条件查询带group by 带order by */ public void moreQueryAndGroupBy() { Map<String, Object> map = new HashMap<String, Object>(); map.put("format", "%Y/%m/%d %H:%M:00"); map.put("date", "$resp_time"); MongoCollection<Document> documents = MongoUtil.instance.getCollection( "lghtest", "detailList"); AggregateIterable<Document> ll = documents.aggregate(Arrays.asList( // new Document("$match", new Document("trans_type", "免费资源查询")), new Document("$group", new Document("_id", new Document( "ret_code", "$ret_code").append("trans_type", "$trans_type").append("resp_time", new Document("$dateToString", new Document(map)))) .append("counts", new Document("$sum", 1))), new Document("$sort", new Document("_id.ret_code", 1)))); for (Document document : ll) { System.out.println(document.toJson()); } }