1

I'm trying to fetch some data from firebase. In my object, I have

Recent
   |_UniversitId
       |_objectiId1
       |  |_ userId:1
       |  |_ timestamp:143242344
       |_objectiId2
          |_ userId:1
          |_ timestamp:143243222

My querying path is http://firbasedbname.com/Recent/UniversityId. I need to fetch the entries which are userId id is equal to 1 and order that set by timestamp. I tried the following,

FIRDatabaseQuery *query = [[firebase queryOrderedByChild:@"userId"] queryEqualToValue:@"1"];

This fetches my the users correctly. But is there a possible way to order this set by timestamp. To do that I tried by putting another queryOrderedByChild. But it says it can be used only one time. How may I fix this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
codebot
  • 2,540
  • 3
  • 38
  • 89
  • As Mohamed says (and David explains in the linked video) you can only order/filter on a single property with the Firebase Database. In your case it seems like you can combine the two values into a single property and filter on that. See my answer here for an example of that: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jun 10 '17 at 16:30
  • I just expanded my answer to that question to include an example with a range query (`startAt` and `endAt`). – Frank van Puffelen Jun 10 '17 at 16:38

1 Answers1

3

queryOrderedByChild can be used only once. a workaround would be

   |_objectiId1
   |  |_ userId:1
   |  |_ timestamp:143242344
   |  |_ userId_timestamp:1_143242344
   |_objectiId2
      |_ userId:1
      |_ timestamp:143243222
      |_ userId_timestamp:1_143243222

Then try :

FIRDatabaseQuery *query = [[firebase queryOrderedByChild:@"userId_timestamp"] queryEqualToValue:@"1_143242344"];

check this out https://youtu.be/sKFLI5FOOHs?t=541


another way to do it would be :
 |_objectiId1
       |_ userId1:
       |      |_ objectId11:143242344
       |      |_ objectId12:143243222
       |_ userId2:    

Then querying path is http://firbasedbname.com/Recent/UniversityId/userId1

and then order by value

  • That's not going to work. The query will read in only the nodes that match 1_1432344 which is probably only 1. Change the query to startingAt:@"1_" and endingAt:@"1_". That will retrieve all of the nodes for user id 1 ordering by the time stamp component of that node. Otherwise a good answer. – Jay Jun 10 '17 at 18:13