这是一个例子。 (注意:firebase中的数据没有改变。这是为了消除一个变量。换句话说,我加载了数据,现在它是静态的。)
我正在测试我是否从orderByChild电话中收到了正确的结果。
我在下面的curl命令中执行此操作。请注意,我正在按照@FrankvanPuffelen的建议对返回值进行排序。
我每次将orderby查询的限制增加一个。
我希望看到上一个查询返回的先前数据和一个新行。
但是我看到的数据显示在一个较大的限制内,并没有达到一个较低的限制。
参见从限制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",
答案 0 :(得分:2)
当您打印整个JSON时,可以找到答案,而不是只打算使用一个属性:
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"
}
}
因此,对于名为publishDateTime
的子/财产,Firebase会在每个帐单下立即 。但遗憾的是,该法案下不存在这样的子/财产,因为该值实际上存储的层次更深。 Firebase索引深度为一级,因此您的索引为空。
当您查询账单时,Firebase会以其他一些未确定的顺序订购账单。
解决方案是摆脱不必要的嵌套。使用push-id(例如-JrERWNEH-hvrBEdd0OR
)来识别账单或使用您自己的ID(例如2015-J2697
)。只要你摆脱了嵌套,这两种方法都有效。