0

Please assist in transforming below MongoDB query in a Spring Data Application.

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'}]}}} 
                ]);

Does anyone know how to add the replaceRoot to my Aggregation?

What we tried,but didnt work

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

Error came as (changed some values or field names intentionally)

{
    "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 Answers0