Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do order by query using firebase [duplicate]

I'm using firebase on my android app. I need help to find an issue to my trouble. I want to do a request like this: I was using the oderbyChild("date") to oder my data from the nearest date to the farest by doing this.

MyApplication.backend.child(urls).orderByChild("value").addValueEventListener(new ValueEventListener() {
}

now i want to this select all where the activity value is done and order theme by my date value. i have write this :

MyApplication.backend.child(urls).orderByChild("task").equalTo("done").addValueEventListener(new ValueEventListener(){
}

My problem is that it's not possible to do 2 two orderByChild on the same fireBase query. How can i fix this ? please need help.

like image 531
Lloyd fairuz Avatar asked Oct 23 '25 10:10

Lloyd fairuz


1 Answers

Assuming your data looks like this

activities
  -JKjasjiji
    date:   20151129
    is_done: false
  -Ykkjaso23
    date:   20151128
    is_done: true
  -Jkaisjiij
    date:   20151127
    is_done: false

There are a few ways to go about this.

1) query for all activities where is_done: true then sort the results in code. This could be very inefficient depending on data set size. Sorting 1 Million in code would be bad.

2) Store the done activities in another node

activities
  -JKjasjiji
    date:   20151129
  -Jkaisjiij
    date:   20151127

done_activities
  -Ykkjaso23
    date:   20151128

3) Store the data in a format that will enable an 'and' type query

activities
  -JKjasjiji
    done_and_date:   false_20151129
  -Ykkjaso23
    done_and_date:   true_20151128
  -Jkaisjiij
    done_and_date:   false_20151127

Then user .startAt and .endAt...

ref.orderByKey().startAt("true_20151128").endAt("true_20151129")

Would give return all activities that are done between the two specified dates.

like image 67
Jay Avatar answered Oct 26 '25 01:10

Jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!