Tritize
Tritize

Reputation: 39

How to organize user's data in FireStore

I would like to create a database on Firestore that would look like this :

Each user have his data (username, ...). But one of his data (ThisListOfData) is a list of things (a list of activities, like yoga, sport, ...). And for each activities (ThisThing1, ...) there is subData. I want to do this like that, because the user can subscribe to more activities, delete some...

It's my first time using Firestore so I have no idea how to do that. The "ThisListOfData" should/can be a collection ? Or a data like a list of string ? If someone could help me on this one, and how to get all the "Things" inside "ThisListOfData" to check the activities the user subscribed to ?

Thanks alot

Upvotes: 1

Views: 433

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7388

When using Firestore or the Realtime Database it is more importand how you "get" the data when you try to figure out "where" and how to "structure" it.

When deciding on this always think of "where will I use this data?" and "how will I query it?".

The structure you shown us is quite OK. What I would recommend is that if there are some data parts that will be accessible also for other users to store those to a separate collection as Users and take a lot of effort to make the collection Users as secure as possible. For that reason to reduce complexity in security rules I always prefer and recommend to store such data (that should be awailable for others) into separate collections.

Also if you want to save something as data field of a document like map or array or if you want to save it as subcollection depends mostly on how you want to get the dat later or if you need to query on it. If you plan to run queries on it and sort such data I would recommend to save it as collection.

Saving data in arrays has some donwsides like updating that works only for the whole array.

Upvotes: 4

Related Questions