I have a Firestore Database that looks like this:
Root
Accounts
username: UID1
Friends
id: SOME_ID
members: {UID1, UID2}
I display all the friends in a RecyclerView
. But how can I use the username
field in the Accounts
collection to query the Friends
List?
CodePudding user response:
With your current data model you'll have to load each friend's user profile separately based on their UID to get their username.
Alternatively you can change your data model to also include the user name for each member in the Friends
document already. While this data duplication may seem unnatural if you come from a background in relational databases, it is really common in NoSQL databases - and one of the reasons they scale so well when reading data.
To learn more about this, also check out:
- This article on NoSQL data modeling
- Todd's excellent video series: Getting to know Cloud Firestore
- My answer on How to write denormalized data in Firebase. While written for Realtime Database, it applies equally to Firestore.