Mark Lisoway
Mark Lisoway

Reputation: 629

Firestore/Angularfire get collection of docs and subcollection of those docs

Is there a way with Firebase or Angularfire2 to get all the docs in a collection, as well as, one or all of the immediate subcollections.

For example, if I have a collection of categories where each category has a name, as well as a subcollection of subcategories, is there a way to get all the categories with their subcategory collection?

In my head, I'd want to do something like this: afs.collection("categories").withSubcollection("subcategories");

Another idea I was thinking of that would work but isn't possible, would be to use a wildcard in the path like this: afs.collection("categories/{categoryId}/subcategories");

I know neither of these are possible, just wanted to see if there was a way to do what I am showing.

At the moment, I store the subcategories in an array on the category document, but documents have a maximum size, a category could theoretically have an unlimited number of subcategories.

Upvotes: 1

Views: 1119

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

Read operations in Firestore are always shallow, and only return document(s) from a single collection. To read documents from multiple collections, you'll need multiple read operations.

The only exception to this is when the collections you want to query have the same name, in which case you can use a collection group query.


I honestly doubt if storing the categories in a subcollection is worth the hassle. While a document has a maximum size, it's 1MB. I'd highly recommend doing the path on how many categories you can store in that, and whether that is a reasonable limit for your app.

Upvotes: 3

Related Questions