title: MongoDB update many with switch fails - The dollar ($) prefixed field '$switch' in 'offerCategoryState.$switch' is not valid for storage
body: <p>I have the following table:</p>
  <p><a href="https://i.stack.imgur.com/jGq3K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jGq3K.png" alt="enter image description here" /></a></p>
  <p>I have to set <code>offerCategoryState</code> column with following rule:</p>
  <ul>
  <li><em><strong>DEPOSIT</strong></em> or <em><strong>INSURANCE</strong></em> -&gt; <em><strong>ENABLED</strong></em></li>
  <li><em><strong>DIGITAL_CARD</strong></em> -&gt; <em><strong>SOON</strong></em></li>
  <li>others -&gt; <em><strong>BLOCKED</strong></em></li>
  </ul>
  <p>I wrote query for doing this with <code>updateMany()</code>:</p>
  <pre><code>db.OFFER_CATEGORIES.updateMany(
      {},
      { $set: { offerCategoryState: { $switch: {
                 branches: [
                     { case: { $eq: [ &quot;$type&quot;, 'DEPOSIT' ] }, then: &quot;ENABLED&quot; },
                     { case: { $eq: [ &quot;$type&quot;, 'INSURANCE' ] }, then: &quot;ENABLED&quot; },
                     { case: { $eq: [ &quot;$type&quot;, 'DIGITAL_CARD' ] }, then: &quot;SOON&quot; },
                 ],
                 default: &quot;BLOCKED&quot;
      } } } }
  );
  </code></pre>
  <p>Unfortunatelly, it fails:</p>
  <blockquote>
  <p>Write error: WriteError{code=52, message='The dollar ($) prefixed field '$switch' in 'offerCategoryState.$switch' is not valid for storage.', details={}</p>
  </blockquote>
  <p><em><strong>How to write such a mongo DB query where setting one column depends on another?</strong></em></p>
  <p>Or even more how to add this new columnt if it doesn't exists at current schema (when set up at new environment).</p>