Search code examples
angularfirebasegoogle-cloud-platformgoogle-cloud-firestoreangularfire

Get firebase firestore nested collection data


I am working on an attendance app and I have a nested collection. correct me or suggest to me if the schema could be better too. Have a look at the images for my current schema. I have to fetch the attendance collection data with sub-collection data to generate the attendance report but when I make the query using angular fire library I get 0 records when I log the result with docs.size or docs.data(). Kindly help me.

this.db.collection("attendance").get().subscribe(docs=>{
      console.log(docs.size);
      docs.forEach(as=>{
        console.log(as.data())
      })
    })

schema-1schema-2 enter image description here


Solution

  • There is no way you can do that. Queries in Firestore are shallow, which means that will only return documents from the collection that the query is run against. There is no way you can get documents from collections and subcollections in a single go. So once you read a document, you only read the document content without the documents that exist in the document's sub-collections.

    To solve this, you have to perform a separate different query for each and every sub-collection, or you can use a collection group query. This means that all sub-collection that exist under each document should have the same name.