MongoDB aggregation with Java driver

后端 未结 4 1230
北恋
北恋 2020-11-29 04:34

I need your help for using MongoDB aggregation framework with java driver. I don\'t understand how to write my request, even with this documentation.

I want to get t

4条回答
  •  鱼传尺愫
    2020-11-29 05:31

    Here is a simple way to count employee by departmentId.. Details at: Aggregation using Java API

            Map empCountMap = new HashMap<>();
    
            AggregateIterable iterable = getMongoCollection().aggregate(Arrays.asList(
                    new Document("$match",
                            new Document("active", Boolean.TRUE)
                                    .append("region", "India")),
                    new Document("$group",
                            new Document("_id", "$" + "deptId").append("count", new Document("$sum", 1)))));
    
            iterable.forEach(new Block() {
                @Override
                public void apply(final Document document) {
                    empCountMap.put((Long) document.get("_id"), (Integer) document.get("count"));
                }
            });
    

提交回复
热议问题