Query data in multiple collections

前端 未结 3 1748
一向
一向 2021-01-28 22:09

I am developing a chat app using Firebase Firestore. I have a collection for room chat and another one for user info. How can I retrieve user data when I listen Room snapshot.

3条回答
  •  星月不相逢
    2021-01-28 22:36

    Firestore does not have the concept of server-side joins or projections across collections. Each query or document read can only take data from a single collection, or from all collections that have the same name with collection group queries. If you need to load data from two collections, you'll need at least two reads.

    So in your case you'll need to load the user data separately, typically caching it in a collection in your code to prevent loading the same user too frequently.

    Another alternative is to duplicate the data that you frequently need from each user into chat documents. This type of duplication is also quite common when modeling data in NoSQL databases.

    For more on these topics, I highly recommend reading NoSQL data modeling and watching getting to know Cloud Firestore.

提交回复
热议问题