我必须满足一些业务要求,我的 key 部分由 Firebase Web api(例如 -JzuZoAtZ9fKY9z3GbEU)创建,部分由手动创建(例如 304810429574385)
我使用的 Firebase 版本:"version": "2.2.7"
我正在使用 orderByKey()
查询对项目进行排序,如 documentation points out :
- Children with a key that can be parsed as a 32-bit integer come first, sorted in ascending order.
- Children with a string value as their key come next, sorted lexicographically in ascending order.
有人可以告诉我为什么 firebase 生成的键不被视为普通字符串,并且它们的位置似乎像数字一样不可预测吗?
两个单独端点的排序键示例:
// case no 1
-JzLOQGFCw6ZwHnQZPFR
-JzuZoAtZ9fKY9z3GbEU
163467300371184
222032364474376
386146869432
// case no 2
222441849
225215973
225484186
-JzpekQUr7jRDHo5qk3D
先前的输出是使用以下方式打印的:
var keys = [];
fireRef.orderByKey().on('child_added',function(snap){
keys.push(snap.key());
});
通过 REST api 获取数据时是一样的,使用 orderBy="$key"
订购时:
GET https://my_app.firebaseio.com/one/two/three.json?orderBy="$key"&auth=superSecret
如果使用 AngularFire,则相同:
$scope.items = $firebaseArray(fireRef.orderByKey());
最佳答案
您的情况 1 中的数字大于 what fits in a 32 bit integer :2147483647
如果将两个列表放在一起,会发生更明显的情况:
222441849
225215973
225484186
-JzLOQGFCw6ZwHnQZPFR
-JzuZoAtZ9fKY9z3GbEU
163467300371184
222032364474376
386146869432
推送 ID 之前的数字(以 -J
开头的键)按其数值排序。推送 ID 之后的数字不适合 32 位整数,因此(如您引用的文档中所述)是 ordered by their lexicographic value .
关于javascript - Firebase orderByKey() 键的顺序不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742832/