1

I'm building ticketing app using Firebase Database. Here's my database format: enter image description here

if I write the code like this:

let ref = FIRDatabase.database().reference()

    ref.child("jadwal")
        .queryOrderedByChild("tanggal")
        .queryEqualToValue("17/08/2016")
        .observeEventType(.ChildAdded, withBlock: { (snapshot) in

        print(snapshot)

    }, withCancelBlock: nil)

the output will be:

Snap (-KPMQefHSuk7j9kb3GTJ) {
   asal = BNA;
   harga = 177000;
   idBus = id;
   jam = "20:00";
   tanggal = "17/08/2016";
   tersedia = 35;
   tujuan = BRN;
}
Snap (-KPMQnCGrXY36NEMxSOU) {
    asal = BNA;
    harga = 150000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 37;
    tujuan = MDN;
}
Snap (-KPMQqFmku_XzItElzjV) {
    asal = BNA;
    harga = 160000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 40;
    tujuan = MDN;
}

it will show all the "tanggal" with the value equal to "17/08/2016", when i do this...

let ref = FIRDatabase.database().reference()

    ref.child("jadwal")
        .queryOrderedByChild("harga")
        .observeEventType(.ChildAdded, withBlock: { (snapshot) in

        print(snapshot)

    }, withCancelBlock: nil)

it will show all the "harga" ordered from low to high like this:

Snap (-KPNgVeXXnqnzevh5w6u) {
     asal = BNA;
     harga = 120000;
     idBus = id;
     jam = "22:30";
     tanggal = "30/08/2016";
     tersedia = 38;
     tujuan = TKG;
}
Snap (-KPMQnCGrXY36NEMxSOU) {
    asal = BNA;
    harga = 150000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 37;
    tujuan = MDN;
}
Snap (-KPMQqFmku_XzItElzjV) {
    asal = BNA;
    harga = 160000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 40;
    tujuan = MDN;
}
Snap (-KPMQefHSuk7j9kb3GTJ) {
    asal = BNA;
    harga = 177000;
    idBus = id;
    jam = "20:00";
    tanggal = "17/08/2016";
    tersedia = 35;
    tujuan = BRN;
}
Snap (-KPNgJPRlEuu0FZvjU2J) {
    asal = BNA;
    harga = 180000;
    idBus = id;
    jam = "21:30";
    tanggal = "25/08/2016";
    tersedia = 40;
    tujuan = MDN;
}
Snap (-KPNgQ2qQXQpY4eT2Jqd) {
    asal = BNA;
    harga = 230000;
    idBus = id;
    jam = "22:30";
    tanggal = "30/08/2016";
    tersedia = 38;
    tujuan = BNJ;
}

see that the "harga" is sorted but i need to show the list of snapshots which shows "harga" is sorted and only "tanggal" is "17/08/2016". i'm always failed to do this and the error mostly with "multiple orderBy()", i need to query and filter data based on two or more child values. Basically I want to sort based on "harga", filter based on "tanggal", "asal", "tujuan", and minimun number of "tersedia" Please help me i want to switch my db from SQL to Firebase. Thank you.

rony
  • 500
  • 1
  • 6
  • 21
  • You can only have a single `orderBy` clause in a Firebase query. Sometimes you can combine the values that you want to filter/order on in a single property, but that depends on your exact use-case. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Aug 19 '16 at 20:26

1 Answers1

1

There is currently no way to query by more than one child key in Firebase. I would suggest two potential solutions to this:

First solution: Flatten your database structure.

Explanation: Instead of just a "jadwal" section with all the children, implement a structure like this:

jadwal: {
    "17/08/2016": {
        harga:
    },
    "17/07/2016": {
        harga:
    }
}

Then, you could choose the path according to which "tanggal" to filter by, then sort the childen by the "harga" value.

Second solution: Do some filtering on client side

Explanation: Do you query in jadwal using the orderBy("harga"), then on the childAdded listener on client side, filter the results. This means you would check if the child has the intended tanggal. If it does, add it to results array. Otherwise, move on to next child.

Let me know if you have any more questions.

Ian Richard
  • 525
  • 4
  • 9
  • could you explain me more about the flatten my database structure? I haven't figure it out yet. Thanks. – rony Aug 25 '16 at 09:31