0

I need three level orderbychild in firebase Android

I already apply two level of orderbychild

mFirebaseDatabase.getReference("childs").orderByChild("busNo").equalTo(no);

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("childs");
mDatabase.orderByChild("childPosition").addValueEventListener(new ValueEventListener() {  }

No such Error but finding the way to do it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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. So in your case that could be a property `"busNo_childPosition": "value1_value2"` that you then order/filter on. For a longer 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 Sep 13 '19 at 13:47

1 Answers1

0

Firebase doesn't support ordering by multiple queries.

Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query throws an error.

Try ordering by a simple query an then filter the data, sample:

Query searchAllQuery = usersRef
            .orderByChild("fullname")
            .startAt(searchBoxInput.toUpperCase())
            .endAt(searchBoxInput.toLowerCase() + "\uf8ff" )

searchAllQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
        // filter your data here by username and country
        User user = dataSnapshot.getValue(User.class);
        if (user.getUsername().contains('wei')  || user.getCoutnry().contains('usa')) {
            // add the user to your list maybe?
        }
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});
Yash
  • 3,438
  • 2
  • 17
  • 33