'How can I query Firestore for related data from a seperate collection?
I hope the title makes some sense to you.
Having come from a relational DB (MySQL) I'm used to being able to get data from a seperate table (collection?) via a field in the current table.
Specifically I want to build my query to gather some user data to support each document based on the userID stored in each document.
My document looks basically like:
Collection: Sites
- addedBy: String (this is a user ID that relates to a document in the "users" collection)
- title: String
- description: String
- etc ...
Collection: Users
- documentID: users ID string (same as addedBy in the sites collection)
- Name: String
- ProfileImage: String
- etc ...
So can I create a query to get the site documents along with their respective users if not are there are solutions to manage this ?
Oh - working in Swift.
Solution 1:[1]
There is no concept of a server-side join that you may know from relational databases.
Firestore read operations return documents from a single collection, or from a group of collections with the same name.
To load from your Sites and Users collection you will have to perform separate read operations for each. Alternatively you can duplicate the data that you most commonly need from a user into the Sites documents. This type of data duplication is quite common in NoSQL databases, and is one of the reasons they scale so well in their read operations.
To learn more about this type of data modeling, I recommend reading NoSQL data modeling and watching the Get to know Cloud Firestore video series.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
