获取所有子集合的列表在Angular中的Firestore

huangapple go评论95阅读模式
英文:

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&lt;any[]&gt; {
    return this.firestore.collection(&#39;businesses&#39;).snapshotChanges().pipe(
      map((businesses) =&gt; {
        return businesses.map((business) =&gt; {
          const businessId = business.payload.doc.id;
          const candidatesRef = this.firestore.collection(
            `businesses/${businessId}/candidates`
          );

          return candidatesRef.snapshotChanges().pipe(
            map((candidates) =&gt; {
              return {
                businessId: businessId,
                candidates: candidates.map((candidate) =&gt; 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&lt;any[]&gt; {
    return this.firestore.collectionGroup(&#39;candidates&#39;).snapshotChanges().pipe(
      map((candidates) =&gt; {...}))

huangapple
  • 本文由 发表于 2023年8月4日 04:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定