title: In mongoDb, how can I rename a field nested like object-> object -> array -> object -> field?
body: <p>Sounds messy,  I know.<br />
  The document I'm modifying is structured like this:</p>
  <pre><code>{
     &quot;_id&quot;:12345,
     &quot;name&quot;:&quot;harold&quot;,
     &quot;basicData&quot;:{
        &quot;devices&quot;:[
           {
              &quot;id&quot;:7654,
              &quot;relatedJson&quot;:{
                 &quot;make&quot;:&quot;sony&quot;,
                 &quot;equipmentID&quot;:&quot;asdf&quot;
              }
           },
           {
              &quot;id&quot;:9493
           }
        ],
        &quot;car&quot;:&quot;toyota&quot;
     }
  }
  </code></pre>
  <p>I can't quite get the code right. This is what I have:</p>
  <pre><code>db.serviceAgreement.updateMany({&quot;basicData.devices.relatedJson.equipmentID&quot;: {$exists: true}},
      [
          {
              $set: {
                  &quot;basicData.devices&quot;: {
                      $map: {
                          input: &quot;$basicData.devices&quot;, in: {
                              $mergeObjects: [&quot;$$this.relatedJson&quot;,
                                  {equipmentId: &quot;$$this.relatedJson.equipmentID&quot;,}]
                          }
                      }
                  }
              }
          },
          {
              $unset: &quot;basicData.devices.relatedJson.equipmentID&quot;
          }
  
      ])
  </code></pre>
  <p>So my understanding of this script is that I'm calling <code>$set</code> to set the field <code>basicData.devices</code>, then I'm setting it to that same list, but before I do that I'm merging the documents <code>relatedJson</code> and a new document <code>{equipmentId : &quot;$$this.relatedJson.equipmentID&quot;,}</code> which has the value <code>equipmentId</code> set to the same value as the <code>equipmentID</code> in the <code>relatedJson</code> object.<br />
  Then, I'm replacing the <code>relatedJson</code> document with the newly created <code>equipmentId</code> (not my intention).<br />
  And finally, deleting the original <code>equipmentID</code> - which doesn't actually exist any more because I've replaced the whole <code>relatedJson</code> object.</p>
  <p>How can I insert the new <code>equipmentId</code> into the <code>relatedJson</code> object, instead of replacing it entirely.</p>
  <p>I have tried variations of the above script that do all sorts of things, inserting a copy of <code>relatedJson</code> into itself, inserting a copy of <code>devices</code> into <code>relatedJson</code>, deleting everything inside <code>devices</code>, but I can't get it to do what I want.<br />
  I feel I'm close to a solution, and maybe I need to modify the <code>$input:</code> but I can't figure out how, or maybe my approach is totally wrong.<br />
  Any help would be greatly appreciated.</p>
  <p>The end result should be the same document, but <code>relatedJson.equipmentID</code> should be renamed <code>relatedJson.equipmentId</code> (with a lower-case 'd' at the end);</p>