Search code examples
node.jsgoogle-cloud-firestoregoogle-cloud-functionswhere-clause

Firebase get with where clause and order of data returned


I have below code in my cloud function. Firestore api call is made to retrieve data that matches a certain document id.

        await myClinics
          .where(FieldPath.documentId(), "in", ["51","32","23","41"])
          .get()
          .then((snapshots) => {
            snapshots.forEach((doc) => {
              // console.log(doc.data());
              
            });
          });

When data is returned, what will be the order of results? will it be of same order as the query (["51","32","23","41"])? Is there a documentation reference relating to this?

I would like to know which data matches document ID 51 and which data matches document ID 32 etc..

This is more of a question.


Solution

  • The documents will be returned in the order in which they occur in the index, not in the order in which you specify the values in the in clause.

    If you want to execute specific code for document 51, check the doc.id value in your code.