firebase-返回多个查询作为collectionData?

发布于 2025-02-09 22:29:20 字数 1677 浏览 2 评论 0 原文

/a>运算符时,当平等数为10或更少时。当它大于10时,我将把数组分为多个长度10的子阵列,并初始化多个查询请求。

当长度小于10时,我只是将 Q 用作查询请求,返回返回collectionData(q,{idfield:'id''}); 。但是,当我拥有 Q0 Q1 等时,我不知道如何以相同的方式返回它们,就像返回 Q

问题是如何返回 Q0 Q1 ,...使用与仅返回 q 的相同语法?

constructor(private firestore: Firestore) {
}

getUserChats() {
    const userId = '12345678910';
    const userRef = doc(this.firestore, `users/${userId}`);
    return docData(userRef).pipe(
        switchMap((data) => {
            let userChats = data.chats;
            let chatsRef = collection(this.firestore, 'chats');
            if (userChats.length <= 10) {
                //use the query normally as the 'in' operator has a limit of 10
                let q = query(chatsRef, where(documentId(), 'in', userChats));
                return collectionData(q, { idField: 'id' });
            } else {
                //breakdown the userChats array into sub-arrays of length 10
                //it is hardcoded for proof-of-concept
                let q0 = query(chatsRef, where(documentId(), 'in', userChats.slice(0, 10)));
                let q1 = query(chatsRef, where(documentId(), 'in', userChats.slice(10, 20)));
                //...
                //how to combine q0 and q1?
                return collectionData(????????????????, { idField: 'id' });
            }
        })
    );
}

The "in" operator works when the number of equality is 10 or less. When it is larger than 10, I will partition the array into multiple sub-arrays of length 10 and initialize multiple query requests.

When the length is less than 10, I am simply using q as the query request and returning return collectionData(q, { idField: 'id' });. However, when I have q0, q1, etc., I don't know how to return them all the same way as if returning q.

The question is how to return q0, q1, ... using the same syntax as just returning q?

constructor(private firestore: Firestore) {
}

getUserChats() {
    const userId = '12345678910';
    const userRef = doc(this.firestore, `users/${userId}`);
    return docData(userRef).pipe(
        switchMap((data) => {
            let userChats = data.chats;
            let chatsRef = collection(this.firestore, 'chats');
            if (userChats.length <= 10) {
                //use the query normally as the 'in' operator has a limit of 10
                let q = query(chatsRef, where(documentId(), 'in', userChats));
                return collectionData(q, { idField: 'id' });
            } else {
                //breakdown the userChats array into sub-arrays of length 10
                //it is hardcoded for proof-of-concept
                let q0 = query(chatsRef, where(documentId(), 'in', userChats.slice(0, 10)));
                let q1 = query(chatsRef, where(documentId(), 'in', userChats.slice(10, 20)));
                //...
                //how to combine q0 and q1?
                return collectionData(????????????????, { idField: 'id' });
            }
        })
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2025-02-16 22:29:20

这是我提出的解决方案:

  • 将数据分为大小 10 (最后一个段可能具有 10 或更少),
  • 为每个查询,使用每个片段的操作员中,其中最多包含 10 平等性,
  • 每个查询都将存储在一个查询中
  • collection> collectiondata 然后用于每个查询中的每个查询返回可观察的
  • 所有可观察物的数组将存储在一个可观察的阵列中。
  • 函数 combineLatest 从rxjs中使用,然后与可观察到的数组一起使用,然后返回
  getUserChats() {
    const userId = 'aabbccdd';
    const userRef = doc(this.firestore, `users/${userId}`);
    return docData(userRef).pipe(
      switchMap((data) => {
        if (data && data.chats) {
          const userChats = data.chats;
          const chatsRef = collection(this.firestore, 'chats');
          let queries = [];
          if (userChats.length !== 0) {
            for (let i = 0; i < userChats.length; i += 10) {
              let q: Query<DocumentData> = query(
                chatsRef,
                where(documentId(), 'in', userChats.slice(i, i + 10))
              );
              queries.push(q);
            }

            let observablesData: Observable<DocumentData[]>[] = [];
            for (let i = 0; i < queries.length; i++) {
              let x = collectionData(queries[i], { idField: 'id' });
              observablesData.push(x);
            }
            return combineLatest(observablesData);
          }
        }
        return [null];
      })
    );
  }

以测试功能,确保检查检查功能 null 案例,因为这将引发一个例外:

this.chatService
      .getUserChats()
      .pipe()
      .subscribe((result: FirebaseChatroom[]) => {
        if (result !== null) {
          result = result.flat();
          //do something with result...
        }
      });

Here is the solution I have come up with:

  • Divide the data into segments of size 10 (the last segment might have 10 or less)
  • For each query, use the in operator for each segment, which contains at most 10 equalities
  • Each query will be stored in an array of queries
  • collectionData is then used for each query in the array, which returns an observable
  • All the observables will be stored inside an array of observable
  • The function combineLatest from RxJS is then used with the array of observables and it then returned
  getUserChats() {
    const userId = 'aabbccdd';
    const userRef = doc(this.firestore, `users/${userId}`);
    return docData(userRef).pipe(
      switchMap((data) => {
        if (data && data.chats) {
          const userChats = data.chats;
          const chatsRef = collection(this.firestore, 'chats');
          let queries = [];
          if (userChats.length !== 0) {
            for (let i = 0; i < userChats.length; i += 10) {
              let q: Query<DocumentData> = query(
                chatsRef,
                where(documentId(), 'in', userChats.slice(i, i + 10))
              );
              queries.push(q);
            }

            let observablesData: Observable<DocumentData[]>[] = [];
            for (let i = 0; i < queries.length; i++) {
              let x = collectionData(queries[i], { idField: 'id' });
              observablesData.push(x);
            }
            return combineLatest(observablesData);
          }
        }
        return [null];
      })
    );
  }

To test the function, ensure to check for the null case, as this will thrown an exception:

this.chatService
      .getUserChats()
      .pipe()
      .subscribe((result: FirebaseChatroom[]) => {
        if (result !== null) {
          result = result.flat();
          //do something with result...
        }
      });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文