title: How to order a firebase collection and then write something to each document?
body: <p>Hey I am new to firebase cloud functions, and I want to order a collection, which is stored in firestore, by a field and then write in each document of the collection the 'position' (this should happen every 60 minutes). After reading the documentations and some suggestions from Stackoverflow I tried following approach.</p>
  <pre><code>exports.scheduledFunctionPosition = functions.pubsub.schedule('every 60 minutes').onRun((context) =&gt; {
      return admin.firestore().collection(&quot;example&quot;)
      .orderBy(&quot;number&quot;, &quot;desc&quot;)
      .get()
      .then(querySnapshot =&gt; {
        if (!querySnapshot.empty) {
          for(let i =0;i&lt;querySnapshot.size;i++){
  
            var queryDocumentSnapshotTmp = querySnapshot.docs[i];
            
  
            queryDocumentSnapshotTmp.ref.update({'position':i+1});
  
          }
          
    
          return null;
    
        } else {
         console.log(&quot;&quot;);
          return null;
        }
      });
  </code></pre>
  <p>As my programming skills aren't too good, I tried it the easy way, but I think there is a problem with the asynchronous programming. This code works perfect for the documents with the highest numbers. But when there are too much documents, this function won't update the last documents. Is there a solution for this?</p>