3

I have Firebase Database structure like this:

enter image description here

i want to get records from index 2 to 5 where language is English.

  FirebaseDatabase.getInstance()
 .getReference("data").orderByChild("language").startAt("English").endAt("English");

its giving all record instead of language English.

what query should be done to retrieve data with single where clause and start and end index or length of response array?

kartikag01
  • 1,539
  • 1
  • 18
  • 36
  • You're trying to create a query that queries with key and the `language` property. That is not possible. Firebase queries can only have a condition on a single property or the key. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jul 10 '16 at 14:33

2 Answers2

0

It is not possible. Firebase queries can only have a condition on a single property or the key.

I have found solution to this problem by altering the structure of data to enter image description here

Then query on different language differently.

 FirebaseDatabase.getInstance()
 .getReference().child("English").orderByKey().startAt("4").limitToFirst(2);

  FirebaseDatabase.getInstance()
 .getReference().child("Hindi").orderByKey().startAt("2").limitToFirst(3);
kartikag01
  • 1,539
  • 1
  • 18
  • 36
0

It's still possible using if condition :

DatabaseReference ref = FirebaseDatabase.getInstance()
   .getReference("data").orderByKey().startAt(2).endAt(5);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
            Quote quote = childSnapshot.getValue(Quote.class);
            if(quote.getLanguage().equals("English")){
                //put your code here
            }
         }                  
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
});
Faruk
  • 5,438
  • 3
  • 30
  • 46