3

I am working with a document database (Google Cloud Firestore) and am trying to get all the data for a collection of deeply nested objects. I have not found a way to ask Firestore for all the data with one simple call.

My Data looks like the following

Assessment: {
    documentid: string
    name: string,
    email: string,
    responses: Response[]
}
Response: {
    documentid: string
    question: string,
    answer: string
}

So I am trying to get all the Assessments from the Assessments Collection -- this brings back all the data but the responses on the Assessment, so I need to make another call to go get that data.

This is the code I am currently using, but it only brings back 1 assessment WITH the responses attached, not the 150 I am expecting.

this.db
  .collection("Assessments")
  .snapshotChanges()
  .pipe(
    //append the documentid to each assessment
    flatMap(actions => {
      return actions.map(a => {
        const assessment = a.payload.doc.data() as Assessment;
        assessment.documentid = a.payload.doc.id;
        return assessment as Assessment;
      });
    }),
    concatMap(assessment => {
      return (
        this.documentDB
          .collection("Assessments")
          .doc(assessment.documentid)
          .collection("Responses")
          .snapshotChanges()
          .pipe(
              //append the documentid to each response
              map(actions => {
                return actions.map(a => {
                  const response = a.payload.doc.data() as WfResponse;
                  response.documentid = a.payload.doc.id;
                  return response;
                });
              }),
              //append the responses to the originial assessment object
              .map(responses => {
                responses.sort(this.sortBySortOrder);
                assessment.responses = responses;
                return assessment;
              })
          );
      );
    })
  )
  .subscribe(assessments => {
    this.queryResult.push(assessments as Assessment);
  });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Doug S.
  • 682
  • 1
  • 10
  • 26

1 Answers1

1

It seems like this is something that isn't supported by Google Cloud Firestore.

This question has been asked in other ways elsewhere, and they seem to consistently confirm that Google Cloud Firestore only supports shallow queries.

See:

https://github.com/GoogleCloudPlatform/google-cloud-php/issues/761

Firestore get all docs and subcollections of root collection

mootrichard
  • 3,581
  • 13
  • 25