繁体   English   中英

如何使用 spring MongoTemplate 在 mongoDB 中将两个字段组合为一个对象?

[英]How combine two fields to one object in mongoDB using spring MongoTemplate?

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

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

使用弹簧数据我们无法实现的方法很少,但解决方案是

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

暂无
暂无

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

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