title: Firestore Update a document after a query
body: <p>I want to insert an object to an existing document (the latest document of the collection).</p>
  <p>I do the query and if the document is not full I insert the new object into the array (by updating the document).</p>
  <pre><code>admin.firestore().collection('LinkPages').orderBy(&quot;sequence&quot;, &quot;desc&quot;).limit(1).get()
     .then(function (linkPagesRef) { 
        linkPagesRef.forEach(function(doc) {
           const linkDocument = doc.data();
              const sequence = doc.data().sequence;
              if(linkDocument.links.length &gt; 19)
              {
                 functions.logger.info(&quot;Document full. Need to create one more&quot;);
              }
              else
              {
                 functions.logger.info(&quot;Document not full. reuse it : &quot;, linkDocument);
                 doc.update({ links: admin.firestore.FieldValue.arrayUnion(youtubeData)})
                 .then(function (docRef) { response.status(200).send(&quot;Ok&quot;); })
                 .catch(function (error) { functions.logger.info(&quot;Error adding document: &quot;, error); response.status(500).send(&quot;Internal error&quot;); });
              }
        });
      })
     .catch(function (error) { functions.logger.info(&quot;Error getting LinksPages document: &quot;, error); response.status(500).send(&quot;Internal error&quot;); });
  </code></pre>
  <p>But At execution I have the error :</p>
  <pre><code>{&quot;severity&quot;:&quot;INFO&quot;,&quot;message&quot;:&quot;Error getting LinksPages document:  TypeError: doc.update is not a function\n
  </code></pre>
  <p>It seems the firestore API is not really consistent and depending on how you get the documents reference you dont have the same available functions.</p>