title: Use a field's value and add it to a new field in an array - Mongodb
body: <p>I am attempting to turn this</p>
  <pre><code>{
     &quot;fooList&quot; : [
        {
           &quot;bar&quot; : {
              &quot;baz&quot; : 100
           }
        },
        {
           &quot;bar&quot; : {
              &quot;baz&quot; : 200
           }
        }
     ]
  },
  {
     &quot;fooList&quot; : [
        {
           &quot;bar&quot; : {
              &quot;baz&quot; : 300
           }
        },
        {
           &quot;bar&quot; : {
              &quot;baz&quot; : 400
           }
        }
     ]
  }
  </code></pre>
  <p>into this:</p>
  <pre><code>{
     &quot;fooList&quot; : [
        {
           &quot;baz&quot; : 100,
           &quot;bar&quot; : {
              &quot;baz&quot; : 100
           }
        },
        {
           &quot;baz&quot; : 200,
           &quot;bar&quot; : {
              &quot;baz&quot; : 200
           }
        }
     ]
  },
  {
     &quot;fooList&quot; : [
        {
           &quot;baz&quot; : 300,
           &quot;bar&quot; : {
              &quot;baz&quot; : 300
           }
        },
        {
           &quot;baz&quot; : 400,
           &quot;bar&quot; : {
              &quot;baz&quot; : 400
           }
        }
     ]
  }
  </code></pre>
  <p>As you can see, I really just copy <code>baz</code> and its value out of <code>bar</code>, but my issue is that is occurs within an array.</p>
  <pre><code>db.getCollection(&lt;collection_name&gt;).updateMany(
      {}, 
      { $set: { 'fooList.$[element].baz' : '$fooList.$[element].bar.baz' } },
      { upsert: true ,
        arrayFilters: [{'element.bar' : { $exists : true }}]
      }
  )
  </code></pre>
  <p>But this will only set the string <code>$fooList.$[element].bar.baz</code> into baz, seen here through the results</p>
  <pre><code>[
    {
      &quot;_id&quot;: ObjectId(&quot;5a934e000102030405000000&quot;),
      &quot;fooList&quot;: [
        {
          &quot;bar&quot;: {
            &quot;baz&quot;: 100
          },
          &quot;baz&quot;: &quot;$fooList.$[element].bar.baz&quot;
        }
      ]
    }
  ]
  </code></pre>
  <p>Can anyone tell me what I might be doing wrong, or if this is even possible? Thanks</p>