title: How to write Replace root and merge objects aggregation operation in spring data
body: <p>I have written a MongoDB native query which is working as expected. I'm trying to write its Spring data equivalent query but unable to do so. Basically, I wanted to filter out array of fields on the basis of titles provided in input array. Here is the sample document:</p>
  
  <pre><code>{
     "domain":"diageotest.com",
     "locale":"en-gb",
     "pageName":"Content_1",
     "contents":[
        {
           "contentName":"Template_1",
           "fields":[
              {
  
                 "id":"firstname",
                 "fieldType":"Plain Text",
                 "title":"First Name",
                 "value":"Pawan"
              },
              {             
                 "id":"lastname",
                 "fieldType":"Plain Text",
                 "title":"Last Name",
                 "value":"Kumar"
              }
           ]
        }
     ],
     "createdBy":"ag-KumarJog",
     "createdDate":"Jan 23,2019",
     "isDefaultLocale":true,
     "modifiedBy":"ag-KumarJog",
     "modifiedDate":"Jan 23,2019",
     "version":1
  }
  </code></pre>
  
  <p>And below is my native query:</p>
  
  <pre><code>db.contents.aggregate([ 
                  {$match: { $and: [
                                  {"domain": "diageotest.com"},
                                  {"locale": {$in: ["en-in", "en-us"]}},
                                  {"contents.contentName": "Template_1"}
                                  ]
                                                  }
                  }, 
                  {$unwind: "$contents"}, 
                  {$unwind: "$contents.fields"}, 
                  {$match: { "contents.fields.title": {$in: ["First Name"]}}}, 
                  {$group: { "_id": "$_id", 
                             "contents":  { "$push": "$contents"}, 
                             "root": {$first:"$$ROOT"} }}, 
                  {$replaceRoot:{newRoot:{$mergeObjects:["$root",{contents:'$contents'}]}}},
                  {$skip : 0},
                  {$limit : 5}
                  ]);
  </code></pre>
  
  <p>Here, in above query, if I pass "First Name" in array ($in condition), then the output JSON will contain only that field whose title is "First Name". </p>
  
  <p>In java, I was trying below but got stuck at replace root:</p>
  
  <pre><code>MatchOperation matchOperation = this.matchByTemplateDomainAndLocales(domainName, templateName,
                  requestBody.getLocales());
  // ---------------------------------------------------//
  UnwindOperation unwindContents = unwind("$contents");
  UnwindOperation unwindFields = unwind("$contents.fields");
  MatchOperation matchFields = match(where("contents.fields.title").in(requestBody.getFields()));
  GroupOperation groupOperation = group("id").push("$contents").as("contents").first("$$ROOT").as("root");
  //ReplaceRootOperation replaceRootOperation = builder().withDocument()
  // ---------------------------------------------------//
  SortOperation sortOperation = sort(sort);
  LimitOperation limitOperation = limit(requestBody.getLimit());
  SkipOperation skipOperation = skip(requestBody.getSkip().longValue());
  </code></pre>
  
  <p>I'm not able to find appropriate methods in replace root that accepts array and perform merge. Kindly help.</p>