1

I'm almost sure that my question has no solution using just firebase, but let me try. I have to get a specific node ordering it by another value, explaining better: I have to get all children "empregado" ordering them by "data". This is my data:

"ponto" : {
"-KEggOdWvibrLfpptyfX" : {
  "data" : "04/04/2016",
  "empregado" : "-KEHl6qtSG7UkZU8Gi4t",
  "entradaM" : "07:03",
  "entradaT" : "12:01",
  "equipe" : "-KDYYDYMqc0l0irnOGjP",
  "horas" : "09:00",
  "horas100" : "",
  "horas50" : "",
  "obra" : "-KDcuiLXj8IgVB5RalLS",
  "observacao" : "tsts",
  "saidaM" : "11:01",
  "saidaT" : "17:00"
},
"-KEggYHd7bBkv04XSx2C" : {
  "data" : "05/04/2016",
  "empregado" : "-KEHl6qtSG7UkZU8Gi4t",
  "entradaM" : "07:01",
  "entradaT" : "12:02",
  "equipe" : "-KDYYDYMqc0l0irnOGjP",
  "horas" : "09:00",
  "horas100" : "",
  "horas50" : "01:00",
  "obra" : "-KDcuiLXj8IgVB5RalLS",
  "observacao" : "tsts",
  "saidaM" : "11:01",
  "saidaT" : "18:00"
},...more data with repeated key "empregado" and not repeated too

So, I did:

ref.child("ponto").orderByChild("empregado").equalTo("-KEHl6qtSG7UkZU8Gi4t").on("child_added", function(snapshot) {
                        ponto = snapshot.val();});

and perfectly I got the data, but I need to order it by child "data", so I thought this could be solved by something like that:

ref.child("ponto").orderByChild("data").orderByChild("empregado").equalTo("-KEHl6qtSG7UkZU8Gi4t").on("child_added", function(snapshot) {
                        ponto = snapshot.val();});

But It says that I can't use twice "orderByChild" Ideas??? Thanks since now.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jorgeley
  • 482
  • 1
  • 6
  • 12

1 Answers1

0

You are correct, your orderByChild defines which field you are query'ing so it cannot be used twice.

Also, using dates like 04/04/2016 may become an issue with sorting later so you may want to store them as 20160404.

Your development platform isn't specified but the data could be loaded in and ordered in code quite easily.

As a solution, you could utilize a combined index

"-KEggOdWvibrLfpptyfX" : {
  "data" : "04/04/2016",
  "empregado" : "-KEHl6qtSG7UkZU8Gi4t",
  "entradaM" : "07:03",
  "entradaT" : "12:01",
  "equipe" : "-KDYYDYMqc0l0irnOGjP"
  "id_date_index": "-KDYYDYMqc0l0irnOGjP_20160404"
},
"-KEggYHd7bBkv04XSx2C" : {
  "data" : "05/04/2016",
  "empregado" : "-KEHl6qtSG7UkZU8Gi4t",
  "entradaM" : "07:01",
  "entradaT" : "12:02",
  "equipe" : "-KDYYDYMqc0l0irnOGjP"
  "id_date_index": "-KDYYDYMqc0l0irnOGjP_20160504"
}

then your query would be

    ref.queryOrderedByChild("id_date_index").queryStartingAtValue("-KDYYDYMqc0l0irnOGjP_0")
        .observeEventType(.ChildAdded, withBlock: { snapshot in

        if ( snapshot.value is NSNull ) {
            print("not found)")

        } else {
            print(snapshot.value)
        }

    })

The concept here is that we know what we want to query for, which in this case is "-KDYYDYMqc0l0irnOGjP_0" so that's static.

The variable is the date so we craft a index that combines the static with the variable

static_variable

Since the variable is numeric and can be sequential we start our query at zero with queryStartingAt:

-KDYYDYMqc0l0irnOGjP_0

which will then retrieve all of the desired nodes, and order them sequentially.

Jay
  • 34,438
  • 18
  • 52
  • 81