I have a simple firestore collection where I need to fetch all the records where the student id matches the key (circled in red in img) inside students.
const q = query(classessRef, where("students.key", "==", "6CgDymxFqFVgTLef2IMhlu5Sxlm2"));
const querySnapshot = await getDocs(q);
Expected Output: Should return all the classes record where students field has 6CgDymxFqFVgTLef2IMhlu5Sxlm2
in it.
2
Answers
I was able to figure out the query to fetch all the records which match the particular key.
What you’re trying to do isn’t excactly possible with the data you have now. That string "6CgDymxFqFVgTLef2IMhlu5Sxlm2" is neither a field nor a value in that document. It’s just part of the path of a field. An example of the full path of a field that you can use in a query is "students.6CgDymxFqFVgTLef2IMhlu5Sxlm2.email".
So, you could use that email field (or any other nested field name under "students.6CgDymxFqFVgTLef2IMhlu5Sxlm2"), if you assume that the field always exists for that student ID. For example:
Or you could create a whole new top-level array type field and add that ID string to it, using an array-contains query to filter for it.
See also: