如何在Firestore中的子集合中侦听所有文档的更改? [英] How do I listen for changes for all documents in a subcollection in firestore?

查看:30
本文介绍了如何在Firestore中的子集合中侦听所有文档的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想听听 messages 集合中所有文档的 chats 集合中的更改,但是找不到允许该方法的方法.

I want to listen to changes in the chats collection for all docs in the messages collection but can't find a method that'll allow that.

  updateUsersListOnChange = loadUsersCallback => {
    return firebase
      .firestore()
      .collection("messages")
      .doc(/* all docs */)
      .collection("chats")
      .onSnapshot(() => {
        loadUsersCallback();
      });
  };

推荐答案

您所描述的内容称为集合组查询,即跨一组集合的查询.

What you're describing is known as a collection group query, a query across a group of collections.

db.collectionGroup("chats").onSnapshot(() => {
    loadUsersCallback();
});

这将从整个数据库中的所有名为 chats 的集合中加载文档.无法指示路径,因此您将要确保您的收藏名称足够独特,以使您可以仅通过其名称来识别权利.

This will load the documents from all collections named chats across your entire database. There is no way to indicate a path, so you'll want to ensure that your collection names are unique enough to allow you to identify the right by by their name alone.

有关更多信息,请参见集合组查询.

For more on this, see the documentation on collection group queries.

这篇关于如何在Firestore中的子集合中侦听所有文档的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆