Vlop
Vlop

Reputation: 53

Filtering documents basing on the documents in their subcollections

Consider the following Firestore structure:

Now, I want to query for people whose pets satisfy certain conditions. Such as, people who have more than two pets, or who own a pet whose name is "ABC".

Is this possible?

Upvotes: 2

Views: 449

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317760

The first example query you listed ("people who have more than two pets") is not possible with your current set of data. There are no counting or aggregate operations provided by Firestore. If you want to count things, you will need to get all the documents and count them on the client, or maintain an active count using some other means.

The second example query you listed ("people who own a pet whose name is "ABC") is possible through a collection group query against the subcollections named "pets" that filters documents using a field that contains the name of the pet. The query will return all of the pets among all of the subcollections, and you will have to iterate them and examine the references to those documents to build the list of people. With your data, it's not possible to issue a single query to get only the list of people.

Upvotes: 3

Related Questions