Retrieve a dynamic value from a subcollection


#1

I would like to be able to retrieve a dynamic value (scores) associated with each one of the elements of my collection by each one of the users. I have already figured out how to estructure things, so right now I have:

\collectionOfItems {
     Item1{
           name: Lorem
           link: ipsum
           scores{
                  user1Uid{
                           value: 69
                           date: today
                  }
           }
     }
      Item2{
            name: Lorem2
            link: ipsum2
            scores{
                   user2Uid{
                            value: 43
                            date: yesterday
                   }
            }
     }
}

\collectionOfUsers {
     user1Uid{
              name: John
              email: john@smith.com
     }
    user2Uid{
              name: mary
              email: mary@johnson.com
     }
}

So I have placed a subcollection within each item named “scores” where each document will have the specific UID of the user saving that data (just the value and the date). I know I can point to that document based on the value in the ds_SlotUserId. But how can I set the dynamic value of each item? Because I have a general sheet with of the information for each item in my collection, but the values inside a subcollection won’t appear there. I guess I have to create an additional data sheet pointing to those values inside “score”, but those values are different depending on the item and the user. How do I do that in React Studio?


#2

Check this: https://www.youtube.com/watch?v=1OZFsgHa3fE&list=PLKrYy47a9BRleRGnLAarv5zkf_2eDUrh6

It’s a chat app but similar structure with collection (chatrooms) and sub collection (chat messages). There are two data sheets; one for Chatrooms and one for selected Chatroom’s Chat messages (this data sheet updates based on the chatroom id data slot value).

P.S. Adding data into sub collection needs to be edited in list or details view. Otherwise it will not work in RS project.

P.S.S To my knowledge you cannot easily list documents and sub collections for each document. Showing one sub collection for selected document in Details view is trivial but having “list inside list” is not possible at least in React Studio (it would also create lot’s of reads in Firestore). Read more e.g. here: http://www.lukasjakob.com/how-to-get-firestore-collection-data-with-sub-collections-in-angular/


#3

Thank you so much for your answer. I see it is a little bit tricky to retrieve the information within a subcollection. Maybe there is a better approach to what I want. I read a couple of articles and saw several videos about noSQL and I created a collection in the root folder named “scores”. Inside that collection, each document has the same ID as the UID for each user and those documents will have fields with the ID of the item and the score they have achieved. It would be something like this:

\collectionOfItems {
     Item1{
           name: Lorem
           link: ipsum
           }
     }
      Item2{
            name: Lorem2
            link: ipsum2
            }
     }
}

\collectionOfUsers {
     user1Uid{
              name: John
              email: john@smith.com
     }
    user2Uid{
              name: mary
              email: mary@johnson.com
     }
}
\scores{
      user1Uid{
              Item1: 68
              Item2: 72
     }
      user2Uid{
              Item1: 93
              Item2: 32
     }
}

So I was wondering if this would work, and as I have a component that shows all the information from each item, I would like that it also shows the score from the user that is logged in at that moment. How could I achieve this?

Thank you.


#4

Well you just need to create Data sheet and query which queries the scores based on user id and use this Data sheet as a data source for the screen.

Here’s how I quickly tested this. I added collection called scores and data sheet which queries the collection is then used as data source for the screen and finally for the component.


#5

Thank you. I understand but I believe my scenario is a little bit tricky. I want the score to appear in a screen that already depends from a different data sheet.

I have a data sheet with all the items within my collection. Each item is shown in a different card (component) where the image, name, label, etc. are on the main datasheet. What I want is that the score for each item that the logged in user has achieved appears in its item. And that relationship is what I am not able to do. I guess it is me not understanding exactly how to link the dots.


#6

Ok then this is more about how to model the data in Firebase. Basibally it’s ”list in a list” which can be achieved by Denormalizing the data into many places with cloud functions or e.g. creating cloud function which returns data based on some ID. This would probably also require that you fill the ”inner” list with the script in the List element’s ”Items array”.

Anyway Firestore has new video (five videos) series about this: https://youtu.be/G04w_k3TPTs