英文:
Get list of all subcollections firestore in angular
问题
我有一个名为"businesses"的Firestore集合,其中有一个名为"candidates"的子集合。如何使用Angular获取所有业务集合的所有候选人集合?在我的服务中,我有以下函数来实现这一目标,但它返回了我想要的内容。
getAllCandidates(): Observable<any[]> {
return this.firestore.collection('businesses').snapshotChanges().pipe(
map((businesses) => {
return businesses.map((business) => {
const businessId = business.payload.doc.id;
const candidatesRef = this.firestore.collection(
`businesses/${businessId}/candidates`
);
return candidatesRef.snapshotChanges().pipe(
map((candidates) => {
return {
businessId: businessId,
candidates: candidates.map((candidate) => candidate.payload.doc.data()),
};
})
);
});
})
);
}
英文:
I have a firestore collection called businesses which has a subcollection called candidates. How do I get all candidates collections for all businesses collections using angular. In my service I have the below function to achieve this but its return what I want
getAllCandidates(): Observable<any[]> {
return this.firestore.collection('businesses').snapshotChanges().pipe(
map((businesses) => {
return businesses.map((business) => {
const businessId = business.payload.doc.id;
const candidatesRef = this.firestore.collection(
`businesses/${businessId}/candidates`
);
return candidatesRef.snapshotChanges().pipe(
map((candidates) => {
return {
businessId: businessId,
candidates: candidates.map((candidate) => candidate.payload.doc.data()),
};
})
);
});
})
);
}
答案1
得分: 1
获取所有businesses
集合下的所有candidates
子集合的方法
由于您知道子集合的名称(即candidates
),您可以使用Collection Group Query:“集合组由具有相同ID的所有集合组成”。
以下代码应该可以实现(未经测试):
getAllCandidates(): Observable<any[]> {
return this.firestore.collectionGroup('candidates').snapshotChanges().pipe(
map((candidates) => {...}))
}
英文:
> How do I get all candidates
[sub]collections for all businesses
collections
Since you know the name of the subcollections (i.e. candidates
) you can use a Collection Group Query: “A collection group consists of all collections with the same ID”.
The following code should do the trick (untested):
getAllCandidates(): Observable<any[]> {
return this.firestore.collectionGroup('candidates').snapshotChanges().pipe(
map((candidates) => {...}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论