Andy Fusniak
Andy Fusniak

Reputation: 1638

How to do a firestore query in chronological order on creation

I built a collection and added some documents sequentially (a few seconds apart). firestore generated each doc.id automatically.

In the documentation it says each doc.id key consists of a timestamp part and random part (presumably to ensure there are no key collisions). Edit: I had read this on an unrelated blog post, so I've removed this to avoid confusion.

Since If the key were to contain some (hidden) chronology, is it possible to do a firestore query based on this key and thus get the result set in ascending or descending chronological order?

Upvotes: 10

Views: 13989

Answers (2)

Alon Shacham
Alon Shacham

Reputation: 299

Made this firebase function to add my missing 'createdAt' field to all of my user documents on firestore. It fixes 450 documents at a time (there's a hard limit on 500) and returns the list of their ids.

const admin = require('firebase-admin');  
admin.initializeApp(/*.....*/);
let db = admin.firestore();

exports.fixUserBase = functions.https.onCall((data, context) => {
      let usersRef = db.collection('users');
      const batch = db.batch();
      let newCount = 0;
      var changed = [];
      return usersRef.get().then(snapshot => {
        if (snapshot.empty) {
          console.log('No matching documents.');
          return [];
        }  
        else {
          console.log('Received ' + snapshot.length + 'user documents.');
        }
        snapshot.forEach(docDataOrg => {
          if (newCount>450){
            if (newCount==451) console.log('Stoping at 450');
            newCount += 1;
          } else {
            var docData = docDataOrg.data();
            var userID = docDataOrg.id;

            var createdAt = docDataOrg["createTime"];
            if (!(docData["createdAt"]!=null)){  
              console.log('updateing createdAt for ' + docData["email"]);
              batch.update(usersRef.doc(userID), {"createdAt":createdAt.seconds})
//I prefer seconds to timestamp, remove the '.seconds' if you prefer otherwise
              newCount += 1;
              changed.push(userID);
            }
          }
        });
        console.log('returning ' + changed);
        return batch.commit().then(function(){
          return changed;
        }); 
      });
    });

This code could have saved me some good hours, happy to share.

Upvotes: 1

Grimthorr
Grimthorr

Reputation: 6926

Unfortunately, Cloud Firestore auto-generated IDs do not provide any automatic ordering, and cannot be relied-upon to be chronological. To order your documents chronologically, you should add your own timestamp field to the documents.

From the Firestore add a document documentation:

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

Upvotes: 18

Related Questions