title: MongoDB $addFields using org.springframework.data.mongodb.core.MongoTemplate
body: <p>How do I go about writing an <code>$addFields</code> query in Spring Data MongoDB Reactive for a simpler and a slightly more complex field addition as shown below:</p>
  
  <pre><code>db.getCollection("mycollection").aggregate(
  [
      { 
          "$addFields" : {
              "existingObjectField.newFieldArray" : [
                  "$existingObjectField.existingFieldObject"
              ]
          }
      },
      { 
          "$addFields" : {
              "existingFieldArray" : {
                  "$map" : {
                      "input" : "$existingFieldArray", 
                      "as" : "item", 
                      "in" : {
                          "existingFieldObject" : {
                              "_id" : "$$item. existingFieldObject._id",
                              "newFieldArray" : [
                                  "$$item. existingFieldObject.existingFieldObject"
                              ]
                          }
                      }
                  }
              }
          }
      },
      { 
          "$out" : "mycollection"
      }
  ]
  </code></pre>
  
  <p>);</p>
  
  <p>In the first add fields, I am simply creating a new array field with one of the existing object field.</p>
  
  <p>In the 2nd add fields, doing the same but within an object in an array in the document.</p>