Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query data in multiple collections

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.

This is my database structure:

enter image description here

enter image description here

Please help

like image 823
imdungnguyen Avatar asked Dec 17 '22 19:12

imdungnguyen


1 Answers

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.

like image 113
Frank van Puffelen Avatar answered Jan 03 '23 20:01

Frank van Puffelen