【问题标题】:How combine two fields to one object in mongoDB using spring MongoTemplate?如何使用spring MongoTemplate将两个字段组合到一个对象中?
【发布时间】:2020-11-23 13:47:27
【问题描述】:

当我想获得两个组合字段时,newAggregation() 中的 project() 应该如何看待?我的收藏包含三个字段:a、b、c。 JS 中的脚本如下所示:

db.collection.aggregate({
        $project:
            {
                "a": "$a",
                "new_field": {
                    "b": "$b",
                    "c": "$c"
                }
            }
    })

【问题讨论】:

  • 这个答案对你有帮助吗

标签: spring mongodb aggregation


【解决方案1】:

使用 Spring Data 无法实现的方法很少,但解决方案是

 Aggregation aggregation=newAggregation(
    p-> new Document("$project",
        new Document()
        .append("a","$a"),
        .append("new_field",
            new Document("b","$b")
            .append("c","$c"),
        )
     )
 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2020-12-20
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多