skip to Main Content

Is it possible to get data of sub-documents by reading only the main document in Firestore? Basically, the Firebase listCollections() method gives the id of subcollections, but how can we get their data?

I have tried for listCollections() to get the id of sub-collections, but have not found a way to get data sub-collections data.

2

Answers


  1. Is it possible to get data of sub-documents by reading only the main document in Firebase?

    In Firestore, the queries are shallow. This means that it can only return documents from the collection that the query is run against. So there is no way you can get documents along with the data that exists inside sub-collections in one go. A single query can only read fields of documents in a single collection.

    If you need to get documents from sub-collection, you need to perform separate queries. If you have collections or sub-collections that have the exact same name then you can use a collection group query.

    Login or Signup to reply.
  2. Firebase listCollectionIds(),Firebase listDocuments() are well documented by firebase but nobody use and write on it….

    I think the best way is to list subcollection to your document throught the well-know get() and then process a foreach on result snapshot

    snapshot.forEach(doc =>{
    console.log(doc.id)
    console.log(doc.data())
    })
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search