3

I want to be able to get the document id of a document that I have just queried with a where clause in firestore. Here is an example that I have at the moment:

db.collection("Users").where('fullname', '==', 'foo bar').limit(5).get()
     .then(function (doc)
    {
        querySnapshot.forEach(function (doc)
        {
          //find document id
        }
    }

From this, I want to be able to get the document id of what I have just queried. Is there any way I can do that?

Thanks in advance.

1 Answers1

7

an example solution to what you're looking for can be found in the Firestore documentation:

db.collection("cities").where("capital", "==", true)
.get()
.then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
    });
})
.catch(function(error) {
    console.log("Error getting documents: ", error);
});

You can simply call doc.id to retrieve the document's ID.

Source: https://cloud.google.com/firestore/docs/query-data/get-data

Bartu
  • 119
  • 8