简体   繁体   中英

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.

  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();
});

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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