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);
});