james wisley
james wisley

Reputation: 119

How to access subcollection in firestore after .where

I have a collection and an array of names and the collection can be queried directly by id

I want to access a unique subcollection depending on a name and I want to return the sub collection after

Query is structured as such

firestore
.collection
.where("name", '==', inputName)
.collection('subCollection')
.get()

This does not seem to work, is there an alternative way

Upvotes: 0

Views: 131

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600100

Firestore queries can only inspect data in the documents that they return. There is no way to return documents from the subcollection, and filter on a field from the parent documents.

The common solution is to add the name to each document in the subcollections too (say as a field called parentName), and then perform a collection group query across all collections named subCollection:

firestore
.collectionGroup('subCollection')
.where("parentName", '==', inputName)
.get()

Upvotes: 3

Related Questions