Firestore: How do I get the doc names of the results of a Firestore query?

919 Views Asked by At

I have this firestore query:

db.collection("cities").where("capital", "==", true)

It returns all documents from the colletion cities that match the where clause, as expected. However, how can I get the names (or: ids?) of the documents that are returned?

I use the query with vue.js and vuexfire like this:

bindCities: firestoreAction(({ bindFirestoreRef }) => {
  var query = db.collection("cities").where("capital", "==", true)
  return bindFirestoreRef("cities", query);
}),
1

There are 1 best solutions below

0
On

doc.id would help you get the document id/name.

db.collection("cities").where("capital", "==", true)
  .get()
  .then(querySnapshot => {
    const documentsID = querySnapshot.docs.map(doc => doc.id)
    // do something with documents
  })