title: Firestore increment replacing or deleting Field
body: <p>I'm trying to use the <a href="https://firebase.google.com/docs/firestore/manage-data/add-data#c_12" rel="nofollow noreferrer">Increment function</a> to keep track of how many alerts of a specific container are stored on Firestore. To do that, on each API call that contains an alert I'm updating this counter by doing this:</p>
  <pre><code>db.collection(&quot;container&quot;)
                          .document(entryDataDto.getCollectionId())
                          .update(&quot;alarmCount&quot;, FieldValue.increment(1));
                          
  </code></pre>
  <p>I have also tried like this:</p>
  <pre><code>Map&lt;String, Object&gt; update = new HashMap&lt;&gt;();
  update.put(&quot;alarmCount&quot;, FieldValue.increment(1));
  
  db.collection(&quot;container&quot;)
                          .document(entryDataDto.getCollectionId())
                          .set(update, SetOptions.merge());
  </code></pre>
  <p>These two snippets of code works seemingly the same but they run into the same problem, they're quite unpredictable.</p>
  <p>From what I've tested 4 things may occur:</p>
  <ol>
  <li>If the field don't already exist it will be created with value 1;</li>
  <li>If the field already exists, it can be replaced with the same field with value 1 (by replaced I literally mean the old field is deleted and a new one take it's place);</li>
  <li>If the field already exists, it can be incremented but the field is deleted right after the value is incremented;</li>
  <li>And finally the rarest of them, if the field already exists, it is incremented by 1.</li>
  </ol>
  <p>These 4 behaviors are quite random and I couldn't figure out a pattern among them. I've only seen number 4 happens one or two times.</p>
  <hr />
  <p>Some pictures of the Firestore data that may be helpful to visualize the issue (sorry I wasn't able to screenshot the events):</p>
  <p><a href="https://i.stack.imgur.com/nlxzo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nlxzo.jpg" alt="Item 2" /></a>
  <strong>Behavior 2</strong></p>
  <p><a href="https://i.stack.imgur.com/5k6ac.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5k6ac.jpg" alt="Item 3" /></a>
  <strong>Behavior 3</strong></p>
  <p><a href="https://i.stack.imgur.com/nEuVO.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nEuVO.jpg" alt="Item 4" /></a>
  <strong>Behavior 4</strong></p>