简体   繁体   English

如何监听 firestore 子集合中所有文档的更改?

[英]How do I listen for changes for all documents in a subcollection in firestore?

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.我想收听messages集合中所有文档的chats集合中的更改,但找不到允许这样做的方法。

  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.您所描述的称为集合组查询,即跨一组 collections 的查询。

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

This will load the documents from all collections named chats across your entire database.这将从整个数据库中的所有 collections 命名chats中加载文档。 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 .有关这方面的更多信息,请参阅有关集合组查询的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM