0

I have posts by users and I want to get all posts of one user in the right order (by the time posted).

So I need something like this:

Query query = reference
                .child("Posts")
                .orderByChild("user_id")
                .equalTo(given_user_id)
                .orderByChild("negative_timestamp");

But that doesn't work because Firebase only allows one orderByChild() call in a Query.

How to solve this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19
  • You can use the ascending and descending direction variable to retrieve posts in any direction you need. – Venkatesh Talacheeru Jun 12 '21 at 10:23
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). Please respond with @AlexMamo – Alex Mamo Jun 12 '21 at 12:27
  • 1
    Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For example, you could create `"user_id-negative_timestamp": "given_user_id-timestamp_value"`, and then filter with `startAt` and `endBefore`. For a longer example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jun 12 '21 at 15:30
  • Thanks for the answer! This helps!! I only have the problem now that I don't use `endAt()` correctly I think. Because it returns 0 posts now. I used this: `query.orderByChild("userid_negativetimestamp").startAt(given_user_id).endAt(given_user_id)` The `startAt()` seems to not cause the problem. It seems that I use `endAt` the wrong way – mathematics-and-caffeine Jun 13 '21 at 20:56
  • The child `userid_negativetimestamp` looks like this: The user ID and then just appended (without space or something) the current timestamp with a leading minus – mathematics-and-caffeine Jun 13 '21 at 20:57
  • I now solved it by this: `.endAt(given_user_id + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzz")` But I dont think this is a good solution xD Is it? – mathematics-and-caffeine Jun 13 '21 at 20:59

0 Answers0