Google Firestore - structuring deeply nested ordered data

后端 未结 2 984
情书的邮戳
情书的邮戳 2021-02-06 08:00

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

  • The app has many sets of exercises
相关标签:
2条回答
  • 2021-02-06 08:40

    Structure it like below. Basically, each exercise is a document and then put the sub-collections under it. In my case shown below, I do something very similar with "products".

    As for ordering, querying, and indexing the data. That should be easy.

    0 讨论(0)
  • 2021-02-06 08:53

    Because all of your data is ordered, you could create your own integer-based exercise ID and query off of it. This ID would be built by a leading "1" then the set #, exercise #, part # and instruction #. Example:

    exerciseID: 102030101
        "text": "First, do x."
        "imageURL": "Set2Exercise3Part1Instruction1.jpg"
    exerciseID: 102030102
        "text": "Second, do y."
        "imageURL": "Set2Exercise3Part1Instruction2.jpg"
    exerciseID: 102030103
        "text": "Third, do z."
        "imageURL": "Set2Exercise3Part1Instruction3.jpg"
    

    Then to query all the instructions, pics, etc. for set #2, exercise #3, part #1 you would include the following line:

    exerciseAppInfo.where("exerciseID", ">=", "102030101").where("exerciseID", "<=", "102030103")
    
    0 讨论(0)
提交回复
热议问题