title: Change object into array in key-value collection
body: <p>I've got a document in MongoDB like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;name&quot;: &quot;wine&quot;,
    &quot;foodstuffSelectedPortions&quot;: {
      &quot;id_1&quot;: [
        {
          &quot;foodstuffId&quot;: &quot;f1&quot;,
          &quot;portion&quot;: {
            &quot;portionName&quot;: &quot;portion name 1&quot;,
            &quot;portionWeight&quot;: {
              &quot;value&quot;: 1,
              &quot;unit&quot;: &quot;KG&quot;
            }
          }
        },
        {
          &quot;foodstuffId&quot;: &quot;f2&quot;,
          &quot;portion&quot;: {
            &quot;portionName&quot;: &quot;portion name 2&quot;,
            &quot;portionWeight&quot;: {
              &quot;value&quot;: 100,
              &quot;unit&quot;: &quot;ML&quot;
            }
          }
        }
      ],
      &quot;id_2&quot;: [
        {
          &quot;foodstuffId&quot;: &quot;f3&quot;,
          &quot;portion&quot;: {
            &quot;portionName&quot;: &quot;portion name 3&quot;,
            &quot;portionWeight&quot;: {
              &quot;value&quot;: 15,
              &quot;unit&quot;: &quot;ML&quot;
            }
          }
        }
      ]
    }
  }
  </code></pre>
  <p>and I want to update <code>foodstuffSelectedPortions.portion</code> object into array that contains this object. So the expected result should look like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;name&quot;: &quot;wine&quot;,
    &quot;foodstuffSelectedPortions&quot;: {
      &quot;id_1&quot;: [
        {
          &quot;foodstuffId&quot;: &quot;f1&quot;,
          &quot;portion&quot;: [
            {
              &quot;portionName&quot;: &quot;portion name 1&quot;,
              &quot;portionWeight&quot;: {
                &quot;value&quot;: 1,
                &quot;unit&quot;: &quot;KG&quot;
              }
            }
          ]
        },
        {
          &quot;foodstuffId&quot;: &quot;f2&quot;,
          &quot;portion&quot;: [
            {
              &quot;portionName&quot;: &quot;portion name 2&quot;,
              &quot;portionWeight&quot;: {
                &quot;value&quot;: 100,
                &quot;unit&quot;: &quot;ML&quot;
              }
            }
          ]
        }
      ],
      &quot;id_2&quot;: [
        {
          &quot;foodstuffId&quot;: &quot;f3&quot;,
          &quot;portion&quot;: [
            {
              &quot;portionName&quot;: &quot;portion name 3&quot;,
              &quot;portionWeight&quot;: {
                &quot;value&quot;: 15,
                &quot;unit&quot;: &quot;ML&quot;
              }
            }
          ]
        }
      ]
    }
  }
  </code></pre>
  <p>I've tried this query:</p>
  <pre class="lang-js prettyprint-override"><code>db.foodstuff.update(
  { },
  { $set: { &quot;foodstuffSelectedPortions.$[].portion&quot;: [&quot;$foodstuffSelectedPortions.$[].portion&quot;] } }
  )
  </code></pre>
  <p>but it gives me an error: <code>Cannot apply array updates to non-array element foodstuffSelectedPortions:</code> which looks fine because the <code>foodstuffSelectedPortions</code> is an object not array.</p>
  <p>How to write this query correctly? I use MongoDB 4.4.4 and Mongo Shell.</p>