I have a database structure as follows (simplified for purposes of this question):
Collection: registrations
-> Document: params = {someParameter: "value"}
-> Document: user_01
-> Sub-collection: orders
-> Document: order_AA = {type: "Q1", address: {pincode: "000000", city:"Paris"}
-> Document: order_AB = {type: "Q2", address: {pincode: "111111", city:"London"}
...
-> Document: user_02
-> Sub-collection: orders
-> Document: order_AC = {type: "Q1", address: {pincode: "222222", city:"Berlin"}
-> Document: order_AD = {type: "Q1", address: {pincode: "333333", city:"Paris"}
...
What I want to do: Obtain a list of all the orders across all users within the collection "registrations". (please note that the number of "user" documents is variable over time, and also the number of "order" documents within the sub-collection)
In more words... Query FireStore to find all the available documents (users) within the collection "registrations", and for each of these documents (users) find all the orders in their sub-collection named "orders".
How can I do this in the shortest steps (fewest queries)?
Also, is it essential for me to add a "dummy" field within each "user" document? I have read on StackOverflow that FireStore cannot query sub-collections unless the parent document has "regular" fields. Even after adding a dummy field, I was unable to get it to work.
My most recent effort has been:
let ordersList = [];
await firestore()
.collection("registrations")
.get()
.then((collectionRef) => {
collectionRef.forEach((userDoc) => {
console.log("==", userDoc.id);
if(userDoc.id.startsWith("user")) {
userDoc.collections("orders")
.get()
.then((ordersSnapshot) => {
ordersSnapshot.forEach((orderDoc) => {
ordersList.push(orderDoc.data());
});
});
}
});
})
.catch(error => {
console.log("Error in retrieving data!");
console.log('Error: ', error);
})
I get the error:
Error: [TypeError: userDoc.collections is not a function. (In 'userDoc.collections("orders")', 'userDoc.collections' is undefined)]
collection
notcollections
, and (2) that the promises from the innerget
s in the loop must be collected and run together with Promise.all – danh