【问题标题】:Invalid reference when grouping by a projected field按投影字段分组时参考无效
【发布时间】:2023-04-08 10:03:01
【问题描述】:

我有两个文件:假设FooQux

Foo 看起来像这样:

{
    "_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
    "name": "Foo 1",
    "description": "This is a Foo",
    "bars": [ 
        {
            "name": "Bar 1",
            "description": "This is a Bar",
            "qux": ObjectId("5c3f3d59d45cca2d1860bb4e")
        },         
        {
            "name": "Bar 2",
            "description": "This is a Bar",
            "qux": ObjectId("5c3f3d59d45cca2d1860bb4e")
        }
    ]
}

虽然Qux 看起来像:

{
    "_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
    "name": "Qux 1",
    "description": "This is a Qux"
}

我的目标是将对应的Qux嵌入到Foo.bars的每个元素中,如下所示:

[{
    "_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
    "name": "Foo 1",
    "description": "This is a Foo",
    "bars": [ 
        {
            "name": "Bar 1",
            "description": "This is a Bar",
            "qux": {
                "_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
                "name": "Qux 1",
                "description": "This is a Qux"
             }
        },         
        {
            "name": "Bar 2",
            "description": "This is a Bar document",
            "qux": {
                "_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
                "name": "Qux 1",
                "description": "This is a Qux"
             }
        }
    ]
}]

我尝试了以下聚合:

Aggregation agg = Aggregation.newAggregation(
    Aggregation.match(Criteria.where("_id").is(id)),
    Aggregation.unwind("bars", true),
    Aggregation.lookup("qux", "bars.qux", "_id", "bars.qux"),
    Aggregation.project("_id", "name", "description")
        .and("bars.qux").arrayElementAt(0).as("bars.qux")
        .and("bars.name").as("bars.name")
        .and("bars.description").as("bars.description"),
    Aggregation.group("_id")
        .push("bars").as("bars")
        .first("name").as("name")
        .first("description").as("description")
);

但由于.push("bars").as("bars")这一行,它会抛出一个IllegalArgumentException

java.lang.IllegalArgumentException: Invalid reference 'bars'!
    at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:100) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:72) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.aggregation.GroupOperation.toDocument(GroupOperation.java:421) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDocument(AggregationOperationRenderer.java:55) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.aggregation.Aggregation.toPipeline(Aggregation.java:645) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.AggregationUtil.createPipeline(AggregationUtil.java:95) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.doAggregate(MongoTemplate.java:2070) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:2046) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1945) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]

如果我在没有 group 操作的情况下执行聚合,它可以工作,但我会为每个 bar 元素得到一个 Foo 并且每个 Foo 包含一个不同的 bar 元素,这是我所期望的,因为我展开它们:

[{
    "_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
    "name": "Foo 1",
    "description": "This is a Foo",
    "bars": {
        "name": "Bar 1",
        "description": "This is a Bar",
        "qux": {
            "_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
            "name": "Qux 1",
            "description": "This is a Qux"
         }
    }
},
{
    "_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
    "name": "Foo 1",
    "description": "This is a Foo",
    "bars": {
        "name": "Bar 2",
        "description": "This is a Bar",
        "qux": {
            "_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
             "name": "Qux 1",
             "description": "This is a Qux"
        }
    }
}]

有没有办法实现我的目标?

【问题讨论】:

    标签: java mongodb spring-data spring-data-mongodb


    【解决方案1】:

    您可以在没有$unwind 的情况下获得所需的输出。一旦我们$lookup 我们可以$map qux rom 通过$indexOfArray$arrayElemAt 加入数组并使用$mergeObjects 合并对象

    db.foo.aggregate([
        {$lookup: {from : "qux", localField : "bars.qux", foreignField : "_id", as : "join"}},
        {$addFields: {bars: {$map : {input : "$bars", as : "b", in : {$mergeObjects :[ "$$b", {qux: {$arrayElemAt : ["$join", {$indexOfArray: ["$join._id", "$$b.qux"]}]}}]}}}}},
        {$project: {join:0}}
    ]).pretty()
    

    输出

    {
            "_id" : ObjectId("5c52bb1af9b7bb512458a6d1"),
            "name" : "Foo 1",
            "description" : "This is a Foo",
            "bars" : [
                    {
                            "name" : "Bar 1",
                            "description" : "This is a Bar",
                            "qux" : {
                                    "_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
                                    "name" : "Qux 1",
                                    "description" : "This is a Qux"
                            }
                    },
                    {
                            "name" : "Bar 2",
                            "description" : "This is a Bar",
                            "qux" : {
                                    "_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
                                    "name" : "Qux 1",
                                    "description" : "This is a Qux"
                            }
                    }
            ]
    }
    

    【讨论】:

    • 这可行,但我需要使用 spring-data-mongodb 在 java 中编写它。
    猜你喜欢
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2010-10-31
    • 2021-05-15
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多