带有“ ReplaceRoot”的Spring Data MongoDB聚合

时间:2019-02-04 09:56:03

标签: spring mongodb spring-data aggregation-framework

请协助在Spring Data Application中在MongoDB查询下面进行转换。

db.test_contents.aggregate([ 
                {$match: { $and: [
                                {"domain": "google.com"},
                                {"locale": {$in: ["en-in", "en-us"]}},
                                {"contents.contentName": "Template1"}
                                ]
                                                }
                }, 
                {$unwind: "$contents"}, 
                {$unwind: "$contents.fields"}, 
                {$match: { "contents.fields.title": {$in: ["Company Name"]}}}, 
                {$group: { "_id": "$_id", 
    "contents":  { "$push": "$contents" }, 
    "root": {$first:"$$ROOT"} }}, 
                {$replaceRoot:{newRoot:{$mergeObjects:["$root",{contents:'$contents'}]}}} 
                ]);

有人知道如何将replaceRoot添加到我的聚合中吗?

我们尝试了什么,但是没有成功

MatchOperation matchOperation = match(where("domain").is("google.com").andOperator(
                where("contents.contentName").is("Template1"),
                where("locale").in(requestBody.getLocales())));

        UnwindOperation unwindOperation = unwind("contents");
        UnwindOperation unwindOperation1 = unwind("contents.fields");
        MatchOperation matchOperation1 = match(where("contents.fields.title").in("First Name"));
        GroupOperation groupOperation = group("id").push("$contents").as("contents").first(Aggregation.ROOT).as("root");

出现错误(故意更改了某些值或字段名称)

{
    "timestamp": "2019-02-06T07:43:04.843+0000",
    "message": "Command failed with error 40400 (Location40400): '$mergeObjects requires object inputs, but input [{contentName: \"Template_1\", fields: {alternateText: \"First Name alternate text.\", description: \"First Name description.\", fieldId: \"firstname\", fieldType: \"Plain Text\", placeholder: \"Enter your first name.\", title: \"First Name\", value: \"Joginder\"}}] is of type array' on server 127.0.0.1:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"$mergeObjects requires object inputs, but input [{contentName: \\\"Template_1\\\", fields: {alternateText: \\\"First Name alternate text.\\\", description: \\\"First Name description.\\\", fieldId: \\\"firstname\\\", fieldType: \\\"Plain Text\\\", placeholder: \\\"Enter your first name.\\\", title: \\\"First Name\\\", value: \\\"Joginder\\\"}}] is of type array\", \"code\" : 40400, \"codeName\" : \"Location40400\" }; nested exception is com.mongodb.MongoCommandException: Command failed with error 40400 (Location40400): '$mergeObjects requires object inputs, but input [{contentName: \"Template_1\", fields: {alternateText: \"First Name alternate text.\", description: \"First Name description.\", fieldId: \"firstname\", fieldType: \"Plain Text\", placeholder: \"Enter your first name.\", title: \"First Name\", value: \"test\"}}] is of type array' on server 127.0.0.1:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"$mergeObjects requires object inputs, but input [{contentName: \\\"Template_1\\\", fields: {alternateText: \\\"First Name alternate text.\\\", description: \\\"First Name description.\\\", fieldId: \\\"firstname\\\", fieldType: \\\"Plain Text\\\", placeholder: \\\"Enter your first name.\\\", title: \\\"First Name\\\", value: \\\"test\\\"}}] is of type array\", \"code\" : 40400, \"codeName\" : \"Location40400\" }",
    "details": "uri=/test/test/template/Template_1/domain/test.com

0 个答案:

没有答案