这是一个例子。 (注意:firebase中的数据没有改变。这是为了消除一个变量。换句话说,我加载了数据,现在它是静态的。)
我正在测试是否从 orderByChild 调用收到正确的结果。
我在下面的curl 命令中执行此操作。请注意,我按照@FrankvanPuffelen 的建议对返回值进行排序。
我每次将 orderby 查询中的限制增加 1。
我希望看到上一个查询返回的上一个数据以及添加的一个新行。
但我看到数据出现在较大的限制中,而不是在较低的限制中。
查看从限制 5 到限制 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,而不是仅 grep 一个属性时,可以找到答案:
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 结构现在可见。您有一个嵌套对象:
bills
-JrERWNEH-hvrBEdd0OR
2015-J2697
fullName
publishDateTime
resolution
我希望您已经像这样定义了索引:
{
"bills": {
".indexOn": "publishDateTime"
}
}
因此,Firebase 将立即在每个帐单下查找名为 publishDateTime
的子项/媒体资源。但不幸的是,该法案中不存在这样的子/属性,因为该值实际上存储得更深一层。 Firebase 索引深一层,因此您的索引为空。
当您随后查询账单时,Firebase 会按照其他一些未确定的顺序对账单进行排序。
解决方案是去掉不必要的嵌套。使用推送 ID(例如 -JrERWNEH-hvrBEdd0OR
)来识别账单或使用您自己的 ID(例如 2015-J2697
)。只要摆脱嵌套,任何一种方法都有效。
关于firebase - 了解 Firebase orderByChild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722481/