title: How to update document in firebase cloud function
body: <p>In my cloud function I want to update my document from 'dashboard' collection when a new student added to 'students' collection.</p>
  
  <pre><code>const getActiveStudents = () =&gt; {
  return db.collection('/students/').where('status', '==', true).get().then(
      snapshot =&gt; {
          let studentsCount = snapshot.docs.length;
          db.collection('/dashboard/').where('type', '==', 'students').get().then(
              result =&gt; {
                  if (result.docs.length === 0) {
                      db.collection('dashboard').add({
                          count: studentsCount,
                          type: 'students',
                          label: 'Active students'
                      });
                  }else {
                      result.docs[0].ref.update({
                          count: studentsCount,
                          type: 'students',
                          label: 'Active students'
                      });
                  }
                  return result;
              }
          ).catch(error =&gt; {
              console.log(error);
          });
          return snapshot;
      }
  ).catch(error =&gt; {
      console.log(error);
  })
  }
  
  exports.onChangesInStudents = functions.firestore.document('/students/{studentId}').onWrite(event =&gt; {
      getActiveStudents();
      return;
  });
  </code></pre>
  
  <p>When I add a new student, instead of updating document it adds a new document to my 'dashboard' collection.
  How should I organize my code in order to properly update the quantity of students.</p>
  
  <p><a href="https://i.stack.imgur.com/vYyBL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vYyBL.png" alt="enter image description here"></a></p>