繁体   English   中英

如何将 map 对象数组转换为 Spring Data Mongo 中的普通值数组

[英]How to map array of objects to array of plain values in Spring Data Mongo

我有一个 collections 文档,它有一个内部对象数组,其中包含两个字段:

{
    "state" : [ 
        {
            "date" : ISODate("2022-08-06T00:56:29.615Z"),
            "value" : 1
        }, 
        {
            "date" : ISODate("2022-08-06T00:56:30.615Z"),
            "value" : 2
        }
    ]
}

我想创建一个这样的投影:

{
    "$project": {
        "value": {
            "$max": {
                "$map": {
                    "input": {
                        "$filter": {
                            "input": "$state",
                            "as": "item",
                            "cond": {
                                "$gte": [
                                    "$$item.date",
                                    new Date("2022-07-05T00:00:00Z")
                                ]
                            }
                        }
                    },
                    "as": "filtered",
                    "in": "$$filtered.value"
                }
            }
        }
    }
}

所以结果我会得到数组过滤项的最大值。

但是,正如我所见, andApply仅支持 AggregationExpression。 有没有办法将简单的字段引用转换为 AggregationExpression?

它可以像这样完成,但似乎最后一个最大值是多余的:

AccumulatorOperators.Max.maxOf(
    VariableOperators.Map.itemsOf(getDatesFilter(onTime)).`as`("filtered")
        .andApply(AccumulatorOperators.Max.maxOf("filtered.value"))
)

似乎我们可以通过 Documents 构造所有“$map”表达式:

AccumulatorOperators.Max.maxOf {
    Document("\$map",
        Document(mapOf(
            "input" to getDatesFilter(onTime).toDocument(),
            "as" to "filtered",
            "in" to "\$\$filtered.value"
        )))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM