【问题标题】:How to fix null value in Spring Mongo group aggregation?如何修复 Spring Mongo 组聚合中的空值?
【发布时间】:2020-06-18 15:14:47
【问题描述】:

对于这个查询

Aggregation aggregation = Aggregation.newAggregation(
        match(Criteria.where("userId").is(accountId)
                .and("processingDay").gte(startMillis).lte(endMillis)),
        sort(Sort.Direction.valueOf("ASC"), "x1"),
        sort(Sort.Direction.valueOf("ASC"),  "x2"),
        Aggregation.group("A", "state")
                .push(new BasicDBObject("x1", "$x1")
                        .append("x2", "$x2")
                        .append("A", "$A")
                ).as("XS"));

结果为@​​987654322@,A为空的地方怎么填?

【问题讨论】:

  • “如何填充一个值为null的值?”:用what值填充?没有null 的典型输出是什么?您是否考虑排除字段A

标签: spring mongodb nosql aggregation-framework spring-data-mongodb


【解决方案1】:

问题是group("A", "state") 创建这样的对象:

"_id":{"A":"...", "state":"..."}

但是,它需要 (Output.class) 根文档中的 A 字段。

只需添加$first 运算符即可在根文档中添加 1stA 值:

Aggregation aggregation = Aggregation.newAggregation(
    match(Criteria.where("userId").is(accountId)
            .and("processingDay").gte(startMillis).lte(endMillis)),
    //sort(Sort.by(Direction.ASC, "x1", "x2")) // explicitly
    sort(Sort.by("x1", "x2")), //Default is ASC
    group("A", "state")
        .first("A").as("A")
        .push(new BasicDBObject("x1", "$x1")
                .append("x2", "$x2")
                .append("A", "$A")
        ).as("XS"));

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2016-03-20
    相关资源
    最近更新 更多