'Get parent collection & sub collection document on firebase

i'm new with firebase i had 2 collections category & product and in product collection it has a sub collection Category , how can i get all document from product collection and sub collection category ? thank in advance enter image description here



Solution 1:[1]

There is no way to read from both the parent collection and the subcollection in one operation. Read always come from one (type of) collection, which is sometimes explained as "all reads in Firestore are shallow".

You can either:

  • Read the parent collection, and then for each (relevant) document read its subcollection as a separate operation.
  • Read the parent collection, and read all SubCategory collections in one go with a collection group query.

The second approach performs fewer calls to the server, but has a higher chance of reading more documents than needed, if (for example) you may not want the SubCategory collection from some documents.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen