Неверная ссылка при группировании по проецируемому полю - PullRequest
0 голосов
/ 31 января 2019

У меня есть два документа: скажем, Foo и Qux.

A 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")
);

Но из-за этой строки выдается IllegalArgumentException .push("bars").as("bars"):

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]

Если я выполняю агрегацию без групповой операции, она работает, но я получаю Foo для каждогоЭлемент bar, и каждый элемент 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"
        }
    }
}]

Есть ли способ достичь моей цели?

1 Ответ

0 голосов
/ 31 января 2019

Вы можете получить желаемый результат без $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()

output

{
        "_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"
                        }
                }
        ]
}
...