What is the correct structure, and how to sort queries

57 views
Skip to first unread message

Matan Yedayev

unread,
Jul 26, 2015, 10:40:07 AM7/26/15
to Firebase Google Group
Hello, I have application of songs and comments.
I have 3 ways of storing this data and I wonder what is the corrent way?
1) All-in-onSongs object, and inside every song there is comment object
2) Comment id inside songIn every song - save his comments as "comment id" and then fetch it from comments 
3) Seperated objectsSeperated object for songs and comments

My second question is how to sort queries?
I want to fetch 10 songs sort by comments number.
Example:
Fetch 3 songs in this order:
1) First song: 227 comments
2) Second song: 138 comments
3) Third song: 122 comments

Next fetch will be:
1) Fourth song: 97 comments
2) Fifth song: 51 comments
3) Sixth song: 11 comments

Thanks you very much people.

Tom Larkworthy

unread,
Jul 26, 2015, 5:27:40 PM7/26/15
to Firebase Google Group
If the comments are expected to always be displayed with the songs I would put the comments in a node below the song. That way to fetch everything on one go. If users often read comments without a song (e.g. view your own comments) than I would flatten the structure and have comments are their own tree. 

To order songs by the number of comments you will need a process to count how many comments there are and write that value in the song record, so then you can use normal sorting by child value. Counts are not out-of-the-box supplied functionality in Firebase (as counts get complicated and slow over large sets).

Hope that helps

Tom 

--
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/b2697766-4a99-420b-8cb6-3724fed3e01f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kato Richardson

unread,
Jul 27, 2015, 12:03:40 PM7/27/15
to fireba...@googlegroups.com
Of course, if you put the comments under the songs, the list of songs will become hard to iterate. So if you will, for example, want to fetch a list of song names for a dropdown, this will require some normalization (moving the list of song names to it's own list) or splitting out the comments to keep the songs payload small. There are always tradeoffs here and no perfect answer.

2) is, for the record, what we recommend for scaling data within the structuring data guide.

☃, Kato

Reply all
Reply to author
Forward
0 new messages