4Manuals

  • PDF Cloud HOME

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

    如何将MongoDb Stitch连接到Android Studio 如何熟悉awsglue和pyspark中的日志 带有mongodb驱动程序3.8.2的Springboot 1.5.7 猫鼬-删除后不会更新“唯一” Unix Shell脚本基于许多输入删除MongoDB文档 如何在嵌套文档中查找记录? 按多个字段排序MongoDB 卡在OAuth 2.0和护照实施中 如何在mongo db中的两个DateString格式的日期之间查找数据? Google Cloud Kubernetes引擎中的MongoDB超时错误

给出Post类型的文档为:

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

我正在从一组帖子中寻找Set<Result>,以使Result对象看起来像:

@Getter
@AllArgsConstructor
class Result {
    String interestId;
    String publisherId;
    Long count;
}

要将操作与java实现进行比较,我正在寻找以下方法的线索:

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());
}

在将其翻译为mongodb查询时,这是我到目前为止所拥有的:

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

这将产生如下文件:

{
    "_id" : {
        "interestId" : "INTEREST1",
        "publisherId" : "PUBLISHER2"
    },
    "count" : 3.0
}

现在,我正在努力解决最终的map操作,以将其表达为预期结果,即:

{
    "_id" : ...
    "interestId" : "INTEREST1",
    "publisherId" : "PUBLISHER2"
    "count" : 3.0
}

如何修复查询以获得预期结果?

1 个答案:

答案 0 :(得分:1)

$project等效于Java的地图:

SerialNumber



Similar searches
    找不到MSExchange自动发现服务 加快Matplotlib循环 MySQL Spatial Join on最近点 并行功能执行而无需在场景之间重置驱动程序? AuthGuard防止Angular Material完全加载