title: Counter triggered when node is deleted/created, but triggers also for changes of children
body: <p>this is the structure of my firebase db:</p>
  
  <pre><code>/UserData
  /DeviceMgmt
    /Counters
        /NumberOfAll:
        /NumberOfSelected
        /TotalDownloaded
      ...
    /Devices
        /pushId1
          /uid
          /toSelect=true (optional)
          /downloaded
          /lastDownload
  
        /pushId2
        /pushId2
         ...
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>exports.countNumberOfAllDevices = functions.database.ref('/UserData/DeviceMgmt/Devices/{pushId}').onWrite(
  (change) =&gt; {
    const collectionRef = change.after.ref;// /UserData/DeviceMgmt/Devices/{pushId}
    const countRef = collectionRef.parent.parent.child('Counters/NumberOfAll');
  
    let increment;
    if (change.after.exists() &amp;&amp; !change.before.exists()) {
      increment = 1;
    } else if (!change.after.exists() &amp;&amp; change.before.exists()) {
      increment = -1;
    } else {
      return null;
    }
  
    return countRef.transaction((current) =&gt; {
      return (current || 0) + increment;
    }).then(() =&gt; {
      return console.log('counter /UserData/Counters/NumberOfAll updated.');
    });
  });
  </code></pre>
  
  <p>is based on <a href="https://github.com/firebase/functions-samples/tree/3b41b2e04d854a821a0149e1e5b0769bad397d67/child-count" rel="nofollow noreferrer">functions-samples/child-count/</a> where pushed messages are replaced by my devices, but my device have children and messages in the example are childless.</p>
  
  <p>My problem is:</p>
  
  <p>1.
  When my device is created in java one writing is object creation, another one is update of its child, so the NumberOfAll counter increments by 2.</p>
  
  <ol start="2">
  <li>At every changes within {pushId} device the function is run useless because that does not change the number of devices.</li>
  </ol>
  
  <p>Should I replace countNumberOfAllDevices with </p>
  
  <p>two functions:</p>
  
  <ol>
  <li><p><code>incrementNumberOfAllDevices</code> with <code>onCreate</code> (after creating device object no child is created later)</p></li>
  <li><p><code>decrementNumberOfAllDevices</code> with <code>onDelete</code> (before deleting device object no child is deleted earlier)</p></li>
  </ol>
  
  <p>OR</p>
  
  <p>go deeper with snapshot reference from </p>
  
  <pre><code>/Devices/{pushId}
  </code></pre>
  
  <p>to</p>
  
  <pre><code>/Devices/{pushId}/uid
  </code></pre>
  
  <p>when uid is never changed between creation and deletion ?</p>
  
  <p>My Java code that triggers the Cloud Function:</p>
  
  <pre class="lang-java prettyprint-override"><code>testAddNewDeviceToDevices(String token) { 
    Device device = new Device( "Test", 0, token); 
    String deviceKey = dbRefDevices.push().getKey(); 
    dbRefDevices.child(deviceKey).setValue(device)
      .addOnFailureListener(new OnFailureListener() { 
        @Override public void onFailure(@NonNull Exception e) { 
          Log.e(TAG, "failed. Exception: ", e); 
        } 
      }); 
    }
  </code></pre>