Firebase multiple WHERE clause in request

I am trying to get data whose flight data is returned only if the arrival date and airport match. I can not find a better solution for this. I can only get data if the date of the airport or arrival is the same, and not both (you can use equalTo() only once). Here's what my current Java code looks like:

 Firebase ref = mFirebaseRef.child(FirebaseReference.CHILD_FLIGHTS); Query queryRef = ref.orderByChild("airport").equalTo(getAirport()); queryRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { System.out.println(TAG + " datasnapshot is equal to " + dataSnapshot); } } 

Here are the data itself:

 { "flight":{ "1ddf3c02-1f2e-4eb7-93d8-3d8d4f9e3da2":{ "airport":"Gothenburg, Sweden - Landvetter (GOT)", "arrivalDate":"2016-06-21", "arrivalTime":"20:58", "code":"GOT", "departureDate":"2016-06-23", "departureTime":"20:58" }, "c2c86e54-b4d0-4d83-934b-775a86f0a16c":{ "airport":"Gothenburg, Sweden - Landvetter (GOT)", "arrivalDate":"2016-06-21", "arrivalTime":"20:50", "code":"GOT", "departureDate":"2016-06-23", "departureTime":"20:50" } }, "users":{ "1ddf3c02-1f2e-4eb7-93d8-3d8d4f9e3da2":{ "age":"25", "createdTime":"1466533088358", "email":"test2@test.com", "provider":"password", "sex":"M", "username":"user2" }, "c2c86e54-b4d0-4d83-934b-775a86f0a16c":{ "age":"25", "createdTime":"1466374588255", "email":"test1@test.com", "provider":"password", "sex":"M", "username":"user1" } } } 

The current Java code will return all children only having the same airport. As you might have guessed, this is not feasible when the amount of data to be sorted on the client side is much larger than the test data above. How can I better filter the data at the end of Firebase?

+6
java android firebase
Jun 21 '16 at 19:12
source share
2 answers

The Realtime database does not support multiple offers, but you can create an additional key to make this possible.

The flight in your "flight" list can have a combination key for "arrivalDate" and "code" .

 "1ddf3c02-1f2e-4eb7-93d8-3d8d4f9e3da2" : { "airport" : "Gothenburg, Sweden - Landvetter (GOT)", "arrivalDate" : "2016-06-21", "arrivalTime" : "20:58", "arrivalDate_code": "2016-06-21_GOT", // combined key "code" : "GOT", "departureDate" : "2016-06-23", "departureTime" : "20:58" } 

Then you can request this key.

 Firebase ref = mFirebaseRef.child(FirebaseReference.CHILD_FLIGHTS); Query queryRef = ref.orderByChild("arrivalDate_code").equalTo("2016-06-21_GOT"); queryRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { } }); 

Another option is to turn off the field in the data structure:

 / flights / $code / $flight_id 

This means that your data will look like this:

 "flight" : { "GOT": { "1ddf3c02-1f2e-4eb7-93d8-3d8d4f9e3da2" : { "airport" : "Gothenburg, Sweden - Landvetter (GOT)", "arrivalDate" : "2016-06-21", "arrivalTime" : "20:58", "code" : "GOT", "departureDate" : "2016-06-23", "departureTime" : "20:58" } } } 

And then you can formulate this request:

 Firebase ref = mFirebaseRef.child(FirebaseReference.CHILD_FLIGHTS); Query queryRef = ref.child("GOT").orderByChild("arrivalTime").equalTo("2016-06-21_GOT"); queryRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { } }); 
+8
Jun 21 '16 at 19:19
source share
— -

Consider the use of nested listeners: First, you can equalTo to one of them, save the results in a variable, and in the nested listener call equalTo the second and check against your results.

This way you can execute the And request.

0
Jun 21 '16 at 19:19
source share



All Articles