Reputation: 1922
I've read almost everywhere about structuring one's Firebase Database
for efficient querying, but I am still a little confused between two alternatives that I have.
For example, let's say I want to get all of a user's "maxBenchPressSessions" from the past 7 days or so.
I'm stuck between picking between these two structures:
In the first array, I use the user's id as an attribute to index on whether true or false. In the second, I use userId
as the attribute NAME whose value would be the user's id.
Is one faster than the other, or would they be indexed a relatively same manner? I kind of new to database design, so I want to make sure that I'm following correct practices.
PROGRESS
I have come up with a solution that will both flatten my database AND allow me to add a ListenerForSingleValueEvent
using orderBy
ONLY once, but only when I want to check if a user has a session saved for a specific day.
I can have each maxBenchPressSession
object have a key in the format of userId_dateString
. However, if I want to get all the user's sessions from the last 7 days, I don't know how to do it in one query.
Any ideas?
Upvotes: 1
Views: 202
Reputation: 116
Then add the user, and it puts all of its sessions.
const ref = firebase.database().ref('maxBenchPressSession/' + userId);
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })
Upvotes: 0
Reputation: 116
As I understand the principle firebase to use it effectively. Should be as small as possible to query the data and it does not matter how many requests.
But you will approach such a request. We'll have to add another field to the database "negativeDate".
This field allows you to get the last seven entries. Here's a video - https://www.youtube.com/watch?v=nMR_JPfL4qg&feature=youtu.be&t=4m36s
.limitToLast(7) - 7 entries
.orderByChild('negativeDate') - sort by date
Example of a request:
const ref = firebase.database().ref('maxBenchPressSession');
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })
Upvotes: 0
Reputation: 116
I recommend to watch the video. It is told about the structuring of the data very well.
References to the playlist on the firebase 3
Upvotes: 1