4

I have a problem:

I would like to create a firebase query using twice the orderByChild() condition

First condition: compare a field

Second condition: order the elements by a specific field.

This is an example of the query that I would doing:

ref.orderByChild("id").equalTo("1").orderByChild("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot d : dataSnapshot.getChildren()) {
        Model m = d.getValue(Model.class);
    }
}

@Override
public void onCancelled(FirebaseError firebaseError) {

    }
});

If I try to use this query the app gets crashed. It's possible to create this type of query in Firebase?

Thanks in advance.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
bbgg2017
  • 187
  • 3
  • 10
  • 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 an 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 Apr 12 '18 at 13:20

2 Answers2

4

Multiple orderbychild() queries is not supported by firebase .

Look at these links to structure your database.

Android firebase apply multiple queries

Firebase how to set orderByChild more than one

Android Firebase multiple sort

Query based on multiple where clauses in Firebase

Sarasa Gunawardhana
  • 1,099
  • 3
  • 14
  • 34
2

No it is not possible to create this kind of query since you cannot use two orderByChild(), the below are possible:

orderByChild("name").equalTo(name);
orderByKey().equalTo("key_here");
orderByChild("name").limitToFirst(10)

more info here:

https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query.html

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134