Sharon
Sharon

Reputation: 3919

How do I retrieve a subcollection of a document in Firestore

I have a Firestore collection with the following structure:

events > default > participants > participant1
                                > participant2
                 > date

etc. So events is a collection, and it has a number of documents with random keys (I've used default as an example). Within "default" there are various fields, but also a subcollection of participants.

When I retrieve events/default I expected to retrieve the list of participants at the same time, but I only get back the fields (date etc) - how do I also get the "participants" collection at the same time?

Upvotes: 0

Views: 38

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317928

Firestore queries are shallow and do not consider documents in collections other than the one you are querying. There are no SQL-like "joins" that merge documents from multiple collections.

If you query events, then you will only ever get documents in that collection. If you want participants, you will need to make an additional query, using the full path of the subcollection, including its parent document ID. For example, in JavaScript:

firestore.collection("events").doc(knownId).collection("participants").get()

Upvotes: 2

Related Questions