I have a collection of companies
and inside of each company document, I have a collection of appointments
. I want to loop through all appointments
of all companies
in a cloud function, so I am using the following collection group query:
db.collectionGroup('appointments')
.get()
.then((querySnapshot: any) => {
querySnapshot.forEach((appointmentDoc: any) => {
const appointment: Appointment = appointmentDoc.data();
appointmentDoc.ref.parent.parent.get().then((companyDoc: any) => {
const company: Company = companyDoc.data();
...
});
});
});
As you can see, in each iteration, I am also getting data for the company that the appointment came from. This works, but I'm concerned about performance. If I have 500 appointments, then isn't this method basically making 501 calls to the database (1 for the appointments and then getting the company data for all 500 appointments)? Is there a better way I can access that parent data so I'm not making all those extra calls? Would be great if I can do this in a way that scales.