Home > Mobile >  Read data from firebase document with sub collections >> sub documents
Read data from firebase document with sub collections >> sub documents

Time:12-03

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.

CodePudding user response:

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.

CodePudding user response:

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())
})

  • Related