Reputation: 539
I have a firestore db but right now it only has a collection of users, with the documents named as the unique user's email they signed up with and then inside the document is the other info.
For the use of displaying the user's profile or editing the profile its working fine. However now that i am starting to add more functionality i'm wondering what is the best way to structure my data. For instance i created a collection called requests and basically a user can request money from some people or they owe some other users money.
So how do i connect the requests collection to the users collection? Should i include a field in the user's document called requests and have a list of all the requests via their document id's? Is this plausible or efficient? Is there a way where i can in my code loop through my list of requests inside the user document and then access the info inside the requests collection by searching for the specific document id? Any ideas as to which is the most efficient way to structure this code and how to access the info across collections that is specific to the user? This issue applies to any other functionality i add for example a chat system.
Upvotes: 0
Views: 1372
Reputation: 138824
I have a firestore db but right now it only has a collection of users, with the documents named as the unique user's email
Usually we are using as document ids for the users, the uid
that is coming from the authentication process and not the email address. The reason is simple, a user can change the email address while the uid
will always be the same. Beside that, the id of the document cannot be changed, unless you recreate the enitre document.
So how do i connect the requests collection to the users collection?
Usually through the uid
.
Should i include a field in the user's document called requests and have a list of all the requests via their document id's?
Yes you can do this way or you can simply duplicate data. You can add a new collection within user object that can hold all requests a user has.
Is this plausible or efficient?
This question cannot be answered since we don't know what's the use-case of your app and how often your documents will be changed. I answered a similar question like yours below:
Is there a way where i can in my code loop through my list of requests inside the user document and then access the info inside the requests collection by searching for the specific document id?
Yes it is, but be aware that everything in Firestore is about the number of reads and writes. So do everything in such a manner that you can have a decent number of read and writes and in the same time you have a very good functionality.
Any ideas as to which is the most efficient way to structure this code and how to access the info across collections that is specific to the user?
This is again about what I have already answered in above post.
Upvotes: 1