title: Cloud Functions for Firebase invoked many times
body: <p>I am just trying out Cloud Functions for Firebase in an effort to move my <code>firebase-queue</code> workers over to the cloud functions. I added a simple function to add a last updated timestamp whenever I create a new node at a given ref. The function looks like this:</p>
  
  <pre><code>var functions = require('firebase-functions');
  const admin = require('firebase-admin');
  
  admin.initializeApp(functions.config().firebase);
  
  exports.setLastUpdatedTimestamp = functions.database.ref('/nodes/{nodeId}')
    .onWrite(event =&gt; {
      const original = event.data.val();
      console.log('Adding lastUpdatedTimestamp to node ', original.name);
      return event.data.ref.child('lastUpdatedAtFromFC').set(Date.now());
    });
  </code></pre>
  
  <p>I deployed this cloud function and added just one node from my app. I went to the Firebase Functions dashboard and see that the function has been invoked 169 times and I have no idea why. When I look at the logs, I see logs like attaching the function to all the past nodes as well.</p>
  
  <p>Is it the case that the <code>onWrite</code> would behave kind of like <code>child_added</code> and run the function for all the existing entities as well?</p>
  
  <p>Would this action be repeated every time I change and deploy the function again?</p>
  
  <p>I was expecting it to run just once for the newly added node.</p>