了解Firebase orderByChild [英] Understanding Firebase orderByChild

查看:45
本文介绍了了解Firebase orderByChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子.(注意:firebase中的数据没有更改.这是为了消除一个变量.换句话说,我加载了数据,现在它是静态的.)

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.)

我正在测试是否从orderByChild调用中收到正确的结果.

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

我在下面的curl命令中执行此操作.请注意,我正在按照@FrankvanPuffelen的建议对返回值进行排序.

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

我每次将orderby查询中的限制增加一.

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.

请参阅从限制5到限制6的过渡:

See the transition from limit 5 to limit 6:

限制1

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

限制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",

限制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",

限制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",

限制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",

限制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",

推荐答案

当您打印整个JSON时,可以找到答案,而不是仅提取一个属性:

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'

赠予:

{
  "-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
    }
  }
}

我删除了一些属性,但是重要的是,现在可以看到完整的JSON结构.您有一个嵌套对象:

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"
  }
}

因此,Firebase将在每个账单下立即查找一个名为 publishDateTime 的孩子/财产.但不幸的是,这样的孩子/财产在账单下不存在,因为该值实际上存储的是更深的一层.Firebase索引深一层,因此您的索引为空.

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.

然后您查询账单时,Firebase会以其他不确定的顺序订购账单.

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

解决方案是摆脱不必要的嵌套.您可以使用推送ID(例如 -JrERWNEH-hvrBEdd0OR )来识别帐单,也可以使用自己的ID(例如 2015-J2697 ).只要您摆脱了嵌套,这两种方法都行得通.

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.

这篇关于了解Firebase orderByChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆