1

Let's say that I have the following structure:

posts {
    post_key_1 {
        user_key: “key”
        message: “message”
    }
    …
}

users {
    user_key_1 {
        location_key: true
    }
    …
}

Each user has a location. How could I get the posts from a certain location (for pagination)? Is there a way to order the posts by the location in the users node?

One way would be to also include the location_key in the post nodes, but if the user decides to change his location and there are a lot of posts, there would be too much workload on the server.

In SQL, it would be something like select * from posts join users using(user_key) where location_key = 1

Thanks.

Toma Radu-Petrescu
  • 2,152
  • 2
  • 23
  • 57
  • Look at this https://firebase.google.com/docs/database/android/lists-of-data – Raz Luvaton Feb 25 '18 at 10:55
  • @RazLuvaton I've looked at that way too many times, but still can't find a solution. What I would need is an equalTo(), but which looks at the value of a different node. – Toma Radu-Petrescu Feb 25 '18 at 10:56
  • Are you developing to android? – Raz Luvaton Feb 25 '18 at 10:58
  • @RazLuvaton iOS, but it's basically the same thing. – Toma Radu-Petrescu Feb 25 '18 at 10:59
  • after a lot of searching, I think the solution is with the property `".indexOn"` in the rules search about it – Raz Luvaton Feb 25 '18 at 11:42
  • @RazLuvaton indexOn is used for efficiency when you know that a key is the one you will usually order by. You still can't select the index as a value of another node, though. – Toma Radu-Petrescu Feb 25 '18 at 11:47
  • "One way would be to also include the location_key in the post nodes, but if the user decides to change his location and there are a lot of posts, there would be too much workload on the server." That is indeed the only way I know of. – Frank van Puffelen Feb 25 '18 at 16:22
  • @Frank van Puffelen that’s pretty unfortunate :( I guess I’ll have to somehow limit the number of posts per user, then... – Toma Radu-Petrescu Feb 25 '18 at 16:30
  • 1
    I wouldn't worry about that too much. Most developers seem to either leave the outdated data unmodified, or casually update it (e.g. in Cloud Functions). See my answer here for the options: https://stackoverflow.com/questions/30693785/how-to-write-denormalized-data-in-firebase – Frank van Puffelen Feb 25 '18 at 16:42

0 Answers0