title: Update certain fields only when current value is undefined with Mongoose
body: <p>If a user deactivates their account we need to perform some updates to different collections. One of these collections is adding an end_date to their mandate and set a stop reason to the assigned EANs (it's a code for an energy meter) which do not have a stop reason already.</p>
  <p>Is it possible to do this update in one call?</p>
  <p>The current call looks like this but the end_date and stop_reason of the assigned_eans inside the tariffs object may not be updated when a value is already set.</p>
  <pre><code>await EnergyMandate.updateMany([{ leadID, end_date: { $exists: false } }, {
      $set: { 
          end_date: Math.floor(Date.now()),
          stop_reason: 'deactivated_account',
  
          'tariffs.electricity.assigned_eans.$[].end_date': Math.floor(Date.now()),
          'tariffs.electricity.assigned_eans.$[].stop_reason': 'deactivated_account',
      
          'tariffs.gas.assigned_eans.$[].end_date': Math.floor(Date.now()),
          'tariffs.gas.assigned_eans.$[].stop_reason': 'deactivated_account',
      }
  }], { session });
  </code></pre>
  <p>[EDIT 1] Updated query based on @nimrod serok's answer</p>
  <pre><code>const mandates = await EnergyMandate.updateMany({ leadID, end_date: { $exists: false } }, {
      $set: {
          end_date: Math.floor(Date.now()),
          stop_reason: 'deactivated_account',
  
          'tariffs.electricity.assigned_eans': {
              $map: {
                  input: &quot;$tariffs.electricity.assigned_eans&quot;,
                  in: {   
                      $mergeObjects: [
                          &quot;$$this&quot;,
                          {
                              end_date: { $ifNull: [&quot;$$this.end_date&quot;, Math.floor(Date.now())] },
                              stop_reason: { $ifNull: [&quot;$$this.stop_reason&quot;, 'deactivated_account'] }
                          }
                      ]
                  }
              }
          },
  
          'tariffs.gas.assigned_eans': {
              $map: {
                  input: &quot;$tariffs.gas.assigned_eans&quot;,
                  in: {   
                      $mergeObjects: [
                          &quot;$$this&quot;,
                          {
                              end_date: { $ifNull: [&quot;$$this.end_date&quot;, Math.floor(Date.now())] },
                              stop_reason: { $ifNull: [&quot;$$this.stop_reason&quot;, 'deactivated_account'] }
                          }
                      ]
                  }
              }
          }
      }
  });
  </code></pre>