title: Update array at specific index by other filed in MongoDB
body: <p>I have a collection, consist of <code>name</code> and <code>data</code>.</p>
  <p><code>data</code> is an array with 2 elements, each element is the object with <code>code</code> and <code>qty</code>.</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd2&quot;),
      &quot;name&quot; : &quot;Anna&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;a&quot;,
              &quot;qty&quot; : 3
          },
          {
              &quot;code&quot; : &quot;b&quot;,
              &quot;qty&quot; : 4
          }
      ]
  },
  {
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd3&quot;),
      &quot;name&quot; : &quot;James&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;c&quot;,
              &quot;qty&quot; : 5
          },
          {
              &quot;code&quot; : &quot;d&quot;,
              &quot;qty&quot; : 6
          }
      ]
  }
  </code></pre>
  <p>I want to update the <code>code</code> of the first element to <code>name</code> of its document. The result I want is</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd2&quot;),
      &quot;name&quot; : &quot;Anna&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;Anna&quot;,
              &quot;qty&quot; : 3
          },
          {
              &quot;code&quot; : &quot;b&quot;,
              &quot;qty&quot; : 4
          }
      ]
  },
  {
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd3&quot;),
      &quot;name&quot; : &quot;James&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;James&quot;,
              &quot;qty&quot; : 5
          },
          {
              &quot;code&quot; : &quot;d&quot;,
              &quot;qty&quot; : 6
          }
      ]
  }
  </code></pre>
  <p>I just google to find how to:</p>
  <ol>
  <li><strong>update array at a specific index</strong> (<a href="https://stackoverflow.com/a/34177929/11738185">https://stackoverflow.com/a/34177929/11738185</a>)</li>
  </ol>
  <pre><code>db.Collection.updateMany(
      { },
      {
          $set:{
              'data.0.code': '$name'
          }
      }
  )
  </code></pre>
  <p>But the code of the first element in <code>data</code> array is a string '$name', not a value (Anna, James)</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd2&quot;),
      &quot;name&quot; : &quot;Anna&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;$name&quot;,
              &quot;qty&quot; : 3
          },
          {
              &quot;code&quot; : &quot;b&quot;,
              &quot;qty&quot; : 4
          }
      ]
  },
  {
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd3&quot;),
      &quot;name&quot; : &quot;James&quot;,
      &quot;data&quot; : [
          {
              &quot;code&quot; : &quot;$name&quot;,
              &quot;qty&quot; : 5
          },
          {
              &quot;code&quot; : &quot;d&quot;,
              &quot;qty&quot; : 6
          }
      ]
  }
  </code></pre>
  <ol start="2">
  <li><strong>update a field by the value of another field</strong>. It takes me to use pipeline updating (<a href="https://stackoverflow.com/a/37280419/11738185">https://stackoverflow.com/a/37280419/11738185</a>): the second param of <code>updateMany</code> is array (pipeline)</li>
  </ol>
  <pre><code>db.Collection.updateMany(
      { },
      [{
          $set:{
              'data.0.code': '$name'
          }
      }]
  )
  </code></pre>
  <p>and It adds field <code>0</code> to each element in <code>data</code> array</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd2&quot;),
      &quot;name&quot; : &quot;Anna&quot;,
      &quot;data&quot; : [
          {
              &quot;0&quot; : {
                  &quot;code&quot; : &quot;Anna&quot;
              },
              &quot;code&quot; : &quot;a&quot;,
              &quot;qty&quot; : 3
          },
          {
              &quot;0&quot; : {
                  &quot;code&quot; : &quot;Anna&quot;
              },
              &quot;code&quot; : &quot;b&quot;,
              &quot;qty&quot; : 4
          }
      ]
  },
  {
      &quot;_id&quot; : ObjectId(&quot;605c666a15d2612ed0afedd3&quot;),
      &quot;name&quot; : &quot;James&quot;,
      &quot;data&quot; : [
          {
              &quot;0&quot; : {
                  &quot;code&quot; : &quot;James&quot;
              },
              &quot;code&quot; : &quot;c&quot;,
              &quot;qty&quot; : 5
          },
          {
              &quot;0&quot; : {
                  &quot;code&quot; : &quot;James&quot;
              },
              &quot;code&quot; : &quot;d&quot;,
              &quot;qty&quot; : 6
          }
      ]
  }
  </code></pre>
  <p>I can't find the solution for this case. Could anyone to help me? How can I update array at fixed index by other field. Thanks for reading!</p>