【问题标题】:Filter array in sub-document array field in Spring frameworkSpring框架中子文档数组字段中的过滤数组
【发布时间】:2017-01-25 08:59:55
【问题描述】:

我正在尝试从 Spring Framework 项目的 MongoDB 中的数组中获取元素。

我找到了MongoDB shell的解决方案,但我不知道如何通过Spring.data.core.aggregation实现它,Spring不支持聚合运算符@addFields之一。

谁能告诉我如何替换这个@addField 或者如何以另一种方式实现它?非常感谢!!!

MongoDB 示例数据:

{
    "_id" : 15,
    "items" : [
            {
                    "columns" : [
                            {
                                    "title" : "hhh",
                                    "value" : 10
                            },
                            {
                                    "title" : "hahaha",
                                    "value" : 20
                            }
                    ]
            },
            {
                    "columns" : [
                            {
                                    "title" : "hiii",
                                    "value" : 50
                            }
                    ]
            }
    ]
}

预期结果:

{
"_id" : 15,
"items" : [
        {
                "columns" : [
                        {
                                "title" : "hahaha",
                                "value" : 20
                        }
                ]
        },
        {
                "columns" : []
        }
]

}

MongoDB Shell的解决方案:

let value = "hahaha";

db.coll.aggregate([
    {
        "$addFields": { 
            "items": { 
                "$map": { 
                    "input": "$items", 
                    "as": "item", 
                    "in": { 
                        "columns": { 
                            "$filter": { 
                                "input": "$$item.columns", 
                                "as": "elt", 
                                "cond": { "$eq": [ "$$elt.title", value ] } 
                            } 
                        }
                    }
                } 
            } 
        } 
    } 
])

MongoDB 版本:3.4.1
春季版:1.4.3

【问题讨论】:

    标签: java spring spring-data aggregation-framework spring-mongo


    【解决方案1】:

    您可以尝试以下方法,但您需要使用 1.8.5 版本。

    Aggregation aggregation = newAggregation(
                project("_id").and(new AggregationExpression() {
                    @Override
                    public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                        DBObject filter = new BasicDBObject("input", "$$item.columns").append("as", "elt").append("cond",
                                new BasicDBObject("$eq", Arrays.<Object>asList("$$elt.title", "hahaha")));
                        DBObject map = new BasicDBObject("input", "$items").append("as", "item").append("in", filter);
                        return new BasicDBObject("$map", map);
                    }
                }).as("items")
      );
    

    在 1.10.0.RC1 中添加了对一些 Mongo3.2 聚合运算符的支持。如果您可以更新到发布候选版本,您可以使用以下版本。我在 RC 中找不到 $addFields 阶段,所以保留了 $project 阶段。

    Aggregation aggregation = newAggregation(
                project("_id")
                        .and(mapItemsOf("items").as("item").andApply(filter("item.columns")
                                .as("elt")
                                .by(valueOf("elt.title").equalToValue("hahaha"))
                        )).as("items")
    );
    

    静态导入:

    import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
    import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
    import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;
    import static org.springframework.data.mongodb.core.aggregation.VariableOperators.mapItemsOf;
    

    【讨论】:

    • 感谢您的建议,但更改版本对我来说太复杂了。
    • Spring boot 1.4.3 有 spring mongo db 1.9.6 版本。所以第一个选项应该适合你。你试过了吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2020-10-16
    • 2023-04-07
    • 2020-04-14
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多