0

I have the following "schema" in Google Firestore. I have a lists collection with different lists and in the lists I have another collection called people.

/COLLECTION_1/ID/COLLECTION_2/ID

/lists/1/people/1

When I get the list document with ID 1, I want to get the count of the people in that list. Like I want to know how many people documents are in the collection for the list with ID 1.

What I don't want:

  • I don't want to fetch all the people and count them in the frontend because I have a lot of lists in my overview and this would be much useless traffic.
  • I don't want to manually calculate the count in the "frontend" and send it on add or update.

Is this possible with Firestore? Are there anything like a view or populated query, or something like aggregations (from mongodb)?

Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
  • There is an operation that lets you count the number of documents that would be returned by a query. https://firebase.google.com/docs/firestore/query-data/aggregation-queries – Doug Stevenson Jul 07 '23 at 17:50
  • Perhaps this [resource](https://medium.com/firebase-tips-tricks/how-to-count-the-number-of-documents-in-a-firestore-collection-3bd0c719978f) will help. – Alex Mamo Aug 14 '23 at 07:18

0 Answers0