Firestore schema design for nested objects

1,014 views
Skip to first unread message

5785...@gmail.com

unread,
Oct 25, 2017, 4:16:40 PM10/25/17
to Firebase Google Group
Hello there, 
since I would like to use Firestore in my next project app, I'm here to ask advices about schema.
My app lets users create their gym training programs and at the moment I built a local database (mobile app) where every program has an array of WEEKS, every week an array of DAYS and every day an array of EXERCISES. This way I can keep track of last training completed and let user continue with the next one.
With this schema in mind, in Firestore should be something like:

Programs (collection)
-Program1 (document)
--Weeks (collection)
---Week1 (document)
----Days (collection)
-----Day1 (document)
------Exercises (collection)
-------Exercise1 (document)
        Exercise2
        Exercise3
        .......
        .......

What do you think ?
As an alternative, I could use an object to represent a Program (with its weeks, days and exercises in it): maybe it would result in less read requests when a user wants to see the entire program but the risk is to exceed the max document size allowed.
Many thanks for any suggestion,
Alessandro


Samuel Stern

unread,
Oct 26, 2017, 11:19:02 AM10/26/17
to fireba...@googlegroups.com
Hi Alessandro,

The schema you have suggested looks good, and it's probably the same suggestion I would have made.  The main consideration here is to consider your reading patterns.  Do you frequently need to load the entire Program at once?  If so this structure will not be efficient.  But if your app needs to load each piece of the program only in response to a new user action (maybe they click into a week first, then into a day, then into an exercise) then your structure will not result in many extra reads.

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/e1c6b895-114c-45bf-9d3e-c33141a0db6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

5785...@gmail.com

unread,
Oct 27, 2017, 2:19:15 AM10/27/17
to Firebase Google Group
Hi Samuel, 
thanks for spending your time helping me: I'm glad that you validated my model!
Kind regards
Alessandro

Rodrigo Sol

unread,
Nov 3, 2017, 3:13:51 PM11/3/17
to fireba...@googlegroups.com
Hi There,

Is there any way to fetch the root document its subdocuments at once?

Thanks

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

5785...@gmail.com

unread,
Nov 3, 2017, 5:23:40 PM11/3/17
to Firebase Google Group
Hello Rodrigo, 
for what I red on documentation, I don't think it's possible and Samuel stressed it in his answer.
What's your schema? Maybe someone could give you an advise.
For example I was exploring the possibility to mix Firestore with Realtime database to satisfy my use case
Reply all
Reply to author
Forward
0 new messages