'How to query firestore collection using reference field
I have 3 firestore collections
I use Angular 13 with @angular/fire ("firebase": "^9.4.0")
Now to get all projects of an user I have this code
import { Firestore, collectionData, collection, docData, doc, getDocs, query, where } from '@angular/fire/firestore';
...
constructor(private firestore: Firestore){}
async getProjects() {
// get user
const user = await firstValueFrom(docData(doc(this.firestore, '/users/ebq3CamTAWN8TFL5JCA6jdwIK2K3')))
// get organizationRef
const organizationRef = doc(this.firestore, user.organization.path)
// get projects
const projects = query(
collection(this.firestore, 'projects'),
where("organizationRef", "==", organizationRef)
)
const querySnapshot = await getDocs(projects)
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
})
}
Is there a way to query this in one shot?
Solution 1:[1]
From your question it seems you want to do something like a join query or something like subquery which is possible with relational databases. With Firestore it is not possible to do something like that. So the task you are doing can not be done using a single query. A Firestore query can only get documents from a single collection at a time. If you need to get data from multiple collections you have to write multiple queries as you did in your code to get the desired result.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prabir |



