0

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. enter image description here

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. enter image description here

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.

Potato
  • 509
  • 1
  • 6
  • 17

1 Answers1

1

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Okay UID part instead of email makes sense, ill try and make that change. But you said I connect collections through uid. So that means inside the request collection i should have like under recipient instead of their name, i'll have their uid, same with requested from field? Also I read on the firebase docs that i should avoid nesting data if possible which is why i wanted to create the field of requests via their document id in the user collection rather than nesting another collection inside. Is my understanding correct? Is what im doing is creating a reference to access in othr collection – Potato Jun 23 '19 at 05:14
  • Flatten your data is always a good approach in a NoSQL world but Firestore allow you to have [nested collections up to 100](https://stackoverflow.com/questions/50944549/how-many-collection-document-pairs-can-be-used-in-firestore/50944699). So if you are using only two, won't be a problem at all. So holding references or having nested collections it's a decision that is up to you to be taken so you can have simple queries to perform. – Alex Mamo Jun 23 '19 at 08:43
  • If i were to nest the collection inside the user collection, is there a point in having a separate duplicate requests collection? Also when i tried to get the list of request document ids from the user collection, i got a error saying that the list was null? ```requests = datasnapshot.data['requests'];``` is what i used to get the list, is this wrong? – Potato Jun 23 '19 at 21:17
  • 1
    You can have a separate collection if your query requires that. Without seeing your code, I cannot say why your request is null. For that, please post another fresh question using a [MCVE](https://stackoverflow.com/help/mcve), so me and other Firebase developers can help you. – Alex Mamo Jun 24 '19 at 06:34
  • I got everything working, however i've read firestores subcollection is subpar as it is not possible or very hard to query for specific information in subcollections for instance if you had a user collection and a nested collection of friends and then their documents with data such as their name, it wouldnt be possible to query for all friends with a given name. I ended up using a subcollection for requests because the other way with document refs was giving me some issues,whats your take on that – Potato Jun 24 '19 at 20:18
  • Oh yes you can using [Collection group queries](https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query). – Alex Mamo Jun 25 '19 at 07:59
  • Ahh okay ill look into it, i just have one question, because i am using stream builders it is much easier to have nested collections because while i am subscribed to the stream i can also easily read from that same collection or stream whereas using ref ids would be more annoying and i'd have to acess to different not nested collections and their documents. So my question is, at this rate i may just have the other stuff nested too like groups, requests, and whatever all nested in users? Is this somethin i shouldnt do and why? How many subcollections is fine and do ref ids take as much power? – Potato Jun 25 '19 at 18:57
  • I can create a new question and show you exactly what i mean but its just the same as my database above except the user will have like 2-3 subcollections for easy querying when creating a streambuilder, just want some guidance on how to do this – Potato Jun 25 '19 at 18:58
  • This is what I recommend you to do, to ask another fresh question and explain the exact use-case, so me or oher Firebase developers can help you. – Alex Mamo Jun 26 '19 at 08:17