I managed to get all the documents and document IDs inside all 'sheets' collection in firestore. Refer to below code -
const baseRef = admin.firestore().collectionGroup("sheets").where("date", "==", date);
const querySnap = await baseRef.get();
const docSnap = querySnap.docs;
docSnap.forEach((doc) => {
console.log("DOC ID ---->>>", doc.id);
// Here I want to get all the required documents from 'members' subcollection present inside this document
});
Now each of these documents contain a subcollection called 'members'. I want only documents from 'members' collection where "memberId" is equal to 'memberId'. How can I get those documents?
I tried the following code but it didn't worked -
const baseRef = admin.firestore().collectionGroup("sheets").where("date", "==", date).collectionGroup("members").where("memberId", "==", memberId);
const querySnap = await baseRef.get();
const docSnap = querySnap.docs;
docSnap.forEach((doc) => {
console.log("DOC ID ---->>>", doc.id);
});
It returned following error -
admin.firestore(..).collectionGroup(..).....is not a function
CodePudding user response:
You cannot define a Query
by calling twice the collectionGroup()
method as you do with:
const baseRef = admin.firestore().collectionGroup("sheets").where("date", "==", date).collectionGroup("members").where("memberId", "==", memberId);
One solution would be to replicate the date
field in the member
documents (i.e. copying the field value from the parent sheet
docs to their member
children docs) and define the Query as follows:
const baseRef = admin.firestore().collectionGroup("members").where("memberId", "==", memberId).where("date", "==", date);
Note that you'll need to build a composite index and that you should not have other subcollections named members
in your database.