了解 Firebase orderByChild
Understanding Firebase orderByChild
这是一个例子。 (注意:firebase 中的数据没有变化。这是为了消除一个变量。换句话说,我加载了数据,现在它是静态的。)
我正在测试是否从 orderByChild 调用中收到了正确的结果。
我在下面的 curl 命令中执行此操作。请注意,我按照@FrankvanPuffelen 的建议对 return 值进行排序。
我每次都将 orderby 查询中的限制增加一个。
我希望看到先前的数据 return 由先前的查询编辑并添加了一个新行。
但我看到数据出现在较大的限制中,而不是在较低的限制中。
查看从极限 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 即可找到答案,而不是只搜索一个 属性:
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
的 child/property。但不幸的是,法案下不存在这样的 child/property ,因为该值实际上存储在更深的一层。 Firebase 索引深一层,因此您的索引为空。
当您随后查询账单时,Firebase 会以其他一些未确定的顺序对账单进行排序。
解决办法是去掉不必要的嵌套。使用推送 ID(例如 -JrERWNEH-hvrBEdd0OR
)来识别账单或使用您自己的 ID(例如 2015-J2697
)。只要您摆脱嵌套,这两种方法都有效。
这是一个例子。 (注意:firebase 中的数据没有变化。这是为了消除一个变量。换句话说,我加载了数据,现在它是静态的。)
我正在测试是否从 orderByChild 调用中收到了正确的结果。
我在下面的 curl 命令中执行此操作。请注意,我按照@FrankvanPuffelen 的建议对 return 值进行排序。
我每次都将 orderby 查询中的限制增加一个。
我希望看到先前的数据 return 由先前的查询编辑并添加了一个新行。
但我看到数据出现在较大的限制中,而不是在较低的限制中。
查看从极限 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 即可找到答案,而不是只搜索一个 属性:
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
的 child/property。但不幸的是,法案下不存在这样的 child/property ,因为该值实际上存储在更深的一层。 Firebase 索引深一层,因此您的索引为空。
当您随后查询账单时,Firebase 会以其他一些未确定的顺序对账单进行排序。
解决办法是去掉不必要的嵌套。使用推送 ID(例如 -JrERWNEH-hvrBEdd0OR
)来识别账单或使用您自己的 ID(例如 2015-J2697
)。只要您摆脱嵌套,这两种方法都有效。