I've got rooms that hold sub-rooms that users can post to. I want to only show user-posts that are less than 24 hours old to my users.
For example :
Cats
kittens
users : 10
posts :
ksjdflkjaslkdjf
userUID : 123
postCaption : "I like kittens"
postTimestamp : 203940340930
So here, if a user went into the Cats room, into the subroom of kittens, and wanted to see posts that have been posted about kittens, I'd only want to return back the posts whose timestamp was less than 24 hours old.
Im not sure about is the most effective way to structure the database. The problem with the way it's set up now is that if there have been 10 million kitten posts, every time the user wants to load up posts firebase would have to go through each post and check the timestamp to see if it's valid or not.
An alternative option might be :
Rooms:
Cats
kittens
users : 10
posts :
post1UID : 209384938942024234 //The value here would be the timestamp
post2UID : 309238942024234
Posts :
209384938942024234
userUID: 123
postCaption : "....."
postTimestamp : 209384938942024234
and I'd query for posts with only a valid timestamp and use the post UID to go grab the posts.
Any advice on the best way to do this before I get started? Thanks in advance.
You could simply query the list of posts to only request the posts from the last 24 hours. While you can't enforce this with security rules , you can at least make your code do the right thing.
But it's quite wasteful: after your app has been live for more than 24 hours, you're querying data that you already know will never be matched.
To prevent this: in NoSQL databases such as Firebase, you often model your data for the specific use-cases of your app. So if you want your users to see a list of post from the last 24 hours, you should model your database to have a list of the posts from the last 24 hours.
You can do this in two ways:
Keep your current structure, but add a secondary index: a list of post IDs for the last day's worth of posts. A simple way to do this is to keep the ID of each post as the key, and its timestamp as the value:
roomId1: post1UID: timestamp post2UID: timestamp
The differene with your approach (at least as far as I saw in my quick scan) is that the index would only have post of the most recent 24 hours, so you wouldn't have to query at all to read the posts (only to clean them up).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.