Google Firestore - Structuring Deeply Nested Ordered Data

I am trying to figure out how to structure my data for my exercise app with Cloud Firestore. The data model is as follows:

  • The application has many sets of exercises.
  • Each exercise set has an ordered exercise list.
  • Each exercise has an ordered list of exercises.
  • Each part of the exercise has an ordered list of instructions.
  • Each command has several properties (e.g. text, imageUrl, etc.)

My first thought was to simply save the data in subsequent subcollections using the numbered keys. However, I need to be able to request all the data from the exercise set (including all exercises, parts, and instructions). Since subcollections are not included in the document query results, saving all the data as subsequent subcomponents will require me to make many queries to retrieve all the data for one set of exercises.

I could also save each of the above as a separate collection of the top level and save a link to each of the parent types (for example, the instruction will have setId, exerciseId and partId). Then I could execute a query for each exercise, parts and instructions, filtering on setId. However, this still requires a few queries, and also requires matching at each level to recover data on the client.

Finally, since I do not expect the need to query the past level of exercises, I could store all the parts and instructions in the exercise document in nested objects / arrays. It smells a bit, plus, when I tried to add several levels of embedded data this way through the web console, firestore threw an error.

Everyone is welcome any guide.

+6

All Articles