Spring mongotemplate:我如何进行字段投影并为字段提供别名

问题描述 投票:0回答:1

我有非常简单的要求,通过 MongoTemplate 我试图过滤并提供输出。在提供输出时,我需要通过别名更改列名称。 ''' 查询查询 = new Query().allowDiskUse(true); query.addCriteria(Criteria.where("column1").is("yes") ..... 现在通过 query.fields().include() 我可以进行投影,但是如何为结果列提供不同的名称,例如别名 '''

spring mongotemplate
1个回答
0
投票

不确定我是否理解正确,但我认为您可以使用简单的聚合管道。

检查此文档以使用投影。

所以我认为你可以尝试这样的事情(未经测试):

Aggregation agg = Aggregation.newAggregation(
    match(Criteria.where("column1").is("yes")),
    project().and("column1").as("your alias here")
);
© www.soinside.com 2019 - 2024. All rights reserved.