Java 计算关系总数并将其映射到结果

Java 计算关系总数并将其映射到结果,java,mongodb,mongodb-query,aggregate,counting,Java,Mongodb,Mongodb Query,Aggregate,Counting,给定Post将文档类型设置为: @Getter @AllArgsConstructor class Post { String publisher; Set<String> interests; } 要将该操作与实现进行比较,我将按照以下方法进行查找: Set<Result> buildQuery(List<Post> postList) { return postList.stream() .flatMap

给定
Post
将文档类型设置为:

@Getter
@AllArgsConstructor
class Post {
    String publisher;
    Set<String> interests;
}

要将该操作与实现进行比较,我将按照以下方法进行查找:

Set<Result> buildQuery(List<Post> postList) {
    return postList.stream()
            .flatMap(post -> post.getInterests().stream()
                    .map(interest -> new AbstractMap.SimpleEntry<>(interest, post.getPublisher())))
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .entrySet().stream()
            .map(e -> new Result(e.getKey().getKey(), e.getKey().getValue(), e.getValue()))
            .collect(Collectors.toSet());
}
这将导致以下文件:

{
    "_id" : {
        "interestId" : "INTEREST1",
        "publisherId" : "PUBLISHER2"
    },
    "count" : 3.0
}
现在,我正在努力解决end
map
操作,以将其表述为一个预期结果,即:

{
    "_id" : ...
    "interestId" : "INTEREST1",
    "publisherId" : "PUBLISHER2"
    "count" : 3.0
}
如何修复查询以获得预期结果?

相当于Java的映射:

db.posts.aggregate([
    {"$unwind" :"$interestIds"},
    {"$group" : {"_id": {interestId: "$interestIds", publisherId:"$publisherId"}, "count": {"$sum" : 1}}},
    {"$project":{ _id: 0, interestId: "$_id.interestId", publisherId: "$_id.publisherId", count: 1 }}
]);
是Java地图的等价物:

db.posts.aggregate([
    {"$unwind" :"$interestIds"},
    {"$group" : {"_id": {interestId: "$interestIds", publisherId:"$publisherId"}, "count": {"$sum" : 1}}},
    {"$project":{ _id: 0, interestId: "$_id.interestId", publisherId: "$_id.publisherId", count: 1 }}
]);
db.posts.aggregate([
    {"$unwind" :"$interestIds"},
    {"$group" : {"_id": {interestId: "$interestIds", publisherId:"$publisherId"}, "count": {"$sum" : 1}}},
    {"$project":{ _id: 0, interestId: "$_id.interestId", publisherId: "$_id.publisherId", count: 1 }}
]);