1

I have firebase like this (I Can't change the table design)

{
  "Visits" : {
    "-LOyE4h8k218xg-SI2Hy" : {
      "Enabled" : false,
      "visitorLocation" : "01",
      "visitorId" : "22"
    },
    "-LOyG4__aiiJ3FBVnWbU" : {
      "Enabled" : false,
      "visitorLocation" : "02",
      "visitorId" : "22"
    },
    "-LOyG5Wyu3cZmZFaCBxY" : {
      "Enabled" : false,
      "visitorLocation" : "03",
      "visitorId" : "22"
    },
    "-LOyG7gkUj8OFdEsyBGS" : {
      "Enabled" : false,
      "visitorLocation" : "04",
      "visitorId" : "22"
    }
  }
}

Stored in the firebase,

I would search using visitorLocation and visitorId, and then change the Enabled value.

what i have right now:

 DatabaseReference ref = 

FirebaseDatabase.getInstance().getReference("visitores");
//This is good, Returns the 4 nodes.
            Query q = ref.orderByChild("visitorId").equalTo("22");


            q.orderByChild("visitorLocation").equalTo("01").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    dataSnapshot.getChildren().iterator().next().getKey();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

but later i get error: You can't combine multiple orderBy calls! can anyone help ? how can i search by multiple key values ?

Thank you !!!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user2274204
  • 315
  • 3
  • 6
  • 18
  • Well, as I see it you are asking for two things to happen simultaneously that can't be done at the same time. You can't for example ask for humans to be ordered by both 'male' and 'female'. That wouldn't be any kind of ordering at all. Perhaps you want sublists of the query with certain expressions? – Jay Snayder Oct 16 '18 at 19:55
  • Why, for example i want to update where visitorLocation=04 && visitorId=22 -> set enabled to true – user2274204 Oct 16 '18 at 20:04
  • It doesn't seem like you are looking for an `order` function call in order to achieve that. Your ordering doesn't seem to matter in your case. But I'll let someone else chime in who works with the databases more often, as I don't work with this frequently. – Jay Snayder Oct 16 '18 at 20:12
  • You could loop through the first query and use something like `if (q->get(visitorId) == 22){//Do Something with q->get('name')}`. – Alchemist Shahed Oct 16 '18 at 22:39
  • 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 example `"visitorId_ visitorLocation": "22_01"`. 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 Oct 17 '18 at 01:08

1 Answers1

1

You can not use "visitorLocation=04 && visitorId=22 " at the same time but you can modify your table structure by combining these two fields like this "visitorLocation_visitoId=04_22" To do so you need to add a field in your firebase database table structure so you can apply this AND query

Khalid Saeed
  • 131
  • 2
  • 8