title: MongoDB: Update array elements on multiple conditions
body: <p>Colleagues, good afternoon!
  I'm struggling with some issue. I am using MongoDB 4.4.4. My assignment looks like this:</p>
  <ol>
  <li>There is a set of elements <code>methods[].subcategories[].actions[]</code>. Please note that all objects are arrays and they may be absent. Elements of the <code>actions[]</code> array consists of the <code>_id</code> and <code>title</code> fields.</li>
  <li>It is necessary to find the actual value of <code>recordId</code> by the field <code>actions.title</code> of the element and write it to the element of the <code>actions</code> array.</li>
  </ol>
  <p>List of current values:</p>
  <pre><code>0af4cd2e-78cb-109b-8178-d5a7ba0e0012, Inspection
  0af4cd2e-78cb-109b-8178-d5a7ba130014, Screening
  0af4cd2e-78cb-109b-8178-d5a7ba170016, Poll
  0af4cd2e-78cb-109b-8178-d5a7ba1b0018, Getting written explanations
  0af4cd2e-78cb-109b-8178-d5a7ba1e001a, Request for documents
  0af4cd2e-78cb-109b-8178-d5a7ba21001c, Sampling (samples)
  0af4cd2e-78cb-109b-8178-d5a7ba23001e, Instrumental examination
  0af4cd2e-78cb-109b-8178-d5a7ba260020, Test
  0af4cd2e-78cb-109b-8178-d5a7ba2b0022, Expertise
  0af4cd2e-78cb-109b-8178-d5a7ba2d0024, Experiment
  3b7205c1-8282-4b63-8121-b82aacd7ca67, Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control
  </code></pre>
  <p>I wrote the following code:</p>
  <pre><code>db.getSiblingDB(&quot;ervk_core&quot;).getCollection(&quot;supervision1&quot;).updateMany(
      {},
      {
          &quot;$set&quot;: {
              &quot;methods.subcategories.actions.$[elem1]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e0012&quot;,
              &quot;methods.subcategories.actions.$[elem2]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e0014&quot;,
              &quot;methods.subcategories.actions.$[elem3]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e0016&quot;,
              &quot;methods.subcategories.actions.$[elem4]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e0018&quot;,
              &quot;methods.subcategories.actions.$[elem5]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e001a&quot;,
              &quot;methods.subcategories.actions.$[elem6]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e001c&quot;,
              &quot;methods.subcategories.actions.$[elem7]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba0e001e&quot;,
              &quot;methods.subcategories.actions.$[elem8]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba260020&quot;,
              &quot;methods.subcategories.actions.$[elem9]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba260022&quot;,
              &quot;methods.subcategories.actions.$[elem10]._id&quot;: &quot;0af4cd2e-78cb-109b-8178-d5a7ba260024&quot;,
              &quot;methods.subcategories.actions.$[elem11]._id&quot;: &quot;3b7205c1-8282-4b63-8121-b82aacd7ca67&quot;,
          }
      },
      {
          &quot;arrayFilters&quot;: [
              {
                  &quot;elem1.title&quot;: &quot;Inspection&quot;,
              },
              {
                  &quot;elem2.title&quot;: &quot;Search&quot;,
              },
              {
                  &quot;elem3.title&quot;: &quot;Poll&quot;,
              },
              {
                  &quot;elem4.title&quot;: &quot;Receipt of Written Explanations&quot;,
              },
              {
                  &quot;elem5.title&quot;: &quot;Retrieval of Documents&quot;,
              },
              {
                  &quot;elem6.title&quot;: &quot;Sampling (samples)&quot;,
              },
              {
                  &quot;elem7.title&quot;: &quot;Instrumental examination&quot;,
              },
              {
                  &quot;elem8.title&quot;: &quot;Trial&quot;,
              },
              {
                  &quot;elem9.title&quot;: &quot;Expertise&quot;,
              },
              {
                  &quot;elem10.title&quot;: &quot;Experiment&quot;,
              },
              {
                  &quot;elem11.title&quot;: &quot;Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control&quot;,
              }
          ]
      }
  );
  </code></pre>
  <p>However, it gives the error <code>&quot;The path 'methods.subcategories.actions' must exist in the document in order to apply array updates.&quot;</code>. I understand why it occurs - due to the absence of the <code>actions[]</code> array. But how can I account for the fact that <code>methods[].subcategories[].actions[]</code> arrays may be missing. And did I write the code correctly, otherwise I'm already a little confused. Thanks a lot in advance!</p>