Understanding Firebase orderByChild

848 views Asked by At

Here's an example. (Note: The data in firebase is NOT changing. This is to eliminate one variable. In other words, I loaded the data and now it is static.)

I'm testing if I'm receiving the correct results from the orderByChild call.

I do this in the curl commands below. Please note that I am sorting the return values as @FrankvanPuffelen suggested.

I increase the limit in the orderby query by one each time.

I expect to see the previous data returned by the previous query and one new row added.

But I see data that appears in a larger limit that was not in the limit one lower.

See the transition from limit 5 to limit 6:

Limit 1

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=1&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",

Limit 2

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=2&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:16:05",

Limit 3

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=3&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:21:05",

Limit 4

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=4&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "publishedDateTime" : "2015-06-05T09:21:05",

Limit 5

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=5&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "publishedDateTime" : "2015-06-05T11:21:11",

Limit 6

$ curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=6&print=pretty'|grep publishedDateTime|sort
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "publishedDateTime" : "2015-06-05T09:21:05",
      **"publishedDateTime" : "2015-06-05T10:36:09",**  **why did this not show in the previous query??**
      "publishedDateTime" : "2015-06-05T11:21:11",
1

There are 1 answers

1
Frank van Puffelen On BEST ANSWER

The answer can be found when you print the entire JSON, instead of grepping just one property:

curl 'https://nysenate.firebaseio.com/bills.json?orderBy="publishedDateTime"&limitToFirst=5&print=pretty'

Gives:

{
  "-JrERWNEH-hvrBEdd0OR" : {
    "2015-J2697" : {
      "fullName" : "Jack M. Martins",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "resolution" : true
    }
  },
  "-JrERWRs0dTPeYJRRtc7" : {
    "2015-J2700" : {
      "fullName" : "Neil D. Breslin",
      "publishedDateTime" : "2015-06-05T09:16:05",
      "resolution" : true
    }
  },
  "-JrERWWNBUJU78Q_ho6p" : {
    "2015-J2705" : {
      "fullName" : "Neil D. Breslin",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "resolution" : true
    }
  },
  "-JrERW_jQto1EX1G8_2w" : {
    "2015-J2712" : {
      "fullName" : "Neil D. Breslin",
      "publishedDateTime" : "2015-06-05T09:21:05",
      "resolution" : true
    }
  },
  "-JrERWdyWKDJujg1d-Ms" : {
    "2015-J2717" : {
      "fullName" : "Gustavo Rivera",
      "publishedDateTime" : "2015-06-05T11:21:11",
      "resolution" : true
    }
  }
}

I've removed some properties, but the important thing is that the complete JSON structure is now visible. You have a nested object:

bills
  -JrERWNEH-hvrBEdd0OR
    2015-J2697
      fullName
      publishDateTime
      resolution

I expect that you've defined your index like this:

{
  "bills": {
    ".indexOn": "publishDateTime"
  }
}

So Firebase will look immediately under each bill for a child/property called publishDateTime. But unfortunately such a child/property does not exist under the bill, since the value is actually stored one level deeper. Firebase indexes are one level deep, so your index is empty.

When you then query the bills, Firebase orders the bills in some other undetermined order.

The solution is to get rid of the unnecessary nesting. Either use push-ids (e.g. -JrERWNEH-hvrBEdd0OR) to identify the bills or use your own ids (e.g. 2015-J2697). Either approach works, as long as you get rid of the nesting.