4

This is My Firebase database structure this my firbase database structure

i want to get data like

category = "cat" and level = "1"

Here is my code

 Query query = firebaseReference.orderByChild("category").equalTo("cat1").limitToFirst(20);

        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                UTILS.Log.e(TAG, "List Of User Count " + dataSnapshot.getChildrenCount());

                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                    //FCreateUSer modelRegister = dataSnapshot1.getValue(FCreateUSer.class);
                    UTILS.Log.e(TAG, "User Email :" + dataSnapshot1.child("question").getValue());

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });

it's wrok but how to use two condition in firbase i try two orderByChild but it's give error java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vanraj Ghed
  • 1,261
  • 11
  • 23
  • A Firebase Database query can only use a single `orderBy` call. You may be able to combine the values into a single property to achieve your use-case. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. – Frank van Puffelen Dec 21 '16 at 15:12

1 Answers1

1

Take a look at this video (it's in JavaScript, but it exactly solves your problem): https://youtu.be/sKFLI5FOOHs?t=612

Basically, you need to structure your data for your queries. In OP example Question will have field like category_level: "cat_1". Then you can perform query equalTo("cat_1"). You can leave out both category and level fields if you need it for other queries (but you will need to handle duplication in that case).

If you know that your number of items is small, simplest solution is to just pull all category=cat and filter level=1 items.

JoKr
  • 4,976
  • 8
  • 27
  • 39