如何在Android中使用startAt(double value,String key)和endAt(double value,String key)Firebase RealTime Database [英] How to use startAt(double value, String key) and endAt(double value, String key) Firebase RealTime Database in Android
问题描述
我想通过使用 orderByChild
进行查询,并使用这些子项的键进行筛选。在这种情况下,我的键和值都是查询我想要的数据。
当键包含数字时,如果键只有字母,那就没有问题了。我试图保持总是字母,因为我直接从firebaser读另一个答案的建议。 >
我的json是:
$ b
{
my_list:{
100xyz:{
value1:100,
value2:100,
name:100xyz
$ b200xyz:{
value1:200,
value2:200,
name:200xyz
},
300xyz:{
value1:300,
value2:300,
name:300xyz
},
400xyz:{
value1:400,
value2:400,
name:400xyz
},
500xyz: {
value1:500,
value2:500,
name:500xyz
}
}
}
我的代码是:
DatabaseReference root = FirebaseDatabase.getInst 。ANCE()getReference()getRoot();
DatabaseReference list = root.child(my_list);
list.orderByChild(value2)。startAt(100,100xyz)。endAt(500,200xyz)。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
SomeModel someModel = dataSnapshot.getValue(SomeModel.class);
Log.d(SOME_MODEL,someModel.getName());
}
});
问题是我可以在控制台中看到4个孩子:
$ $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b
但我只能看到2
D / SOME_MODEL:100xyz
D / SOME_MODEL:200xyz
我想强调:只要键没有数字就行了。
如何通过orderByChild和键同时过滤键有数字吗?
您似乎认为传递两个参数到 endAt()
使查询结束在值或键优先的地方。这不是查询在Firebase中的工作方式。
第二个参数是 endAt()
找到匹配第一个参数的节点。所以当你传递 endAt(500,200xyz)
时,数据库首先在节点上过滤 value2
等于 value2
(第一个参数)。这意味着它发现这个节点作为查询的结束:
500xyz:{
value1: 500,
value2:500,
name:500xyz
}
然后使用键 200xyz
(第二个参数)来确定是否包含此节点(技术上说:在多个节点的列表中结束返回)。由于键 200xyz
在 500xyz
之前,查询结果不包含此节点,导致:
$ b $ pre $ 100 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'名称:100xyz
},
200xyz:{
value1:200,
value2:200,
name 200xyz
,
300xyz:{
value1:300,
value2:300,
name:300xyz
$ b400xyz:{
value1:400,
value2:400,
name:400xyz
},
另见:
$ b $ Firebase orderByChild with startAt()的b
I want to do a query by using orderByChild
and also filtering with the key of those child. In this case, my keys and values are both data that works for querying what I want.
My problem is presented when the keys contains numbers, if the keys only have letters, then there is no problem. I'm trying to maintain always letters along the numbers since I read the recommendation in another answer directly from a firebaser.
My json is:
{
"my_list":{
"100xyz":{
"value1": 100,
"value2": 100,
"name":"100xyz"
},
"200xyz":{
"value1": 200,
"value2": 200,
"name":"200xyz"
},
"300xyz":{
"value1": 300,
"value2": 300,
"name":"300xyz"
},
"400xyz":{
"value1": 400,
"value2": 400,
"name":"400xyz"
},
"500xyz":{
"value1": 500,
"value2": 500,
"name":"500xyz"
}
}
}
My code is:
DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
DatabaseReference list = root.child("my_list");
list.orderByChild("value2").startAt(100, "100xyz").endAt(500, "200xyz").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
SomeModel someModel = dataSnapshot.getValue(SomeModel.class);
Log.d("SOME_MODEL", someModel.getName());
}
});
The problem is I can see 4 childs in the console:
D/SOME_MODEL: 100xyz
D/SOME_MODEL: 200xyz
D/SOME_MODEL: 300xyz
D/SOME_MODEL: 400xyz
But I should only be seen 2
D/SOME_MODEL: 100xyz
D/SOME_MODEL: 200xyz
I want to emphazise: it does works, as long as the keys don't have numbers.
How can I filter by orderByChild and key at the same time when the keys have numbers?
You seem to think that passing two arguments to endAt()
makes the query end at whichever of the value or key comes first. That is not how queries work in Firebase.
The second argument to endAt()
is only used after the query finds nodes that match the first argument.
So when you're passing endAt(500, "200xyz")
, the database first filters on nodes with value2
equal to value2
(the first argument). This means it finds this node as the end of the query:
"500xyz":{
"value1": 500,
"value2": 500,
"name":"500xyz"
}
It then uses the key 200xyz
(the second argument) to determine whether to include this node or not (technically: where in the list of multiple nodes to end returning). Since the key 200xyz
is before 500xyz
the query result doesn't include this node, resulting in:
"100xyz":{
"value1": 100,
"value2": 100,
"name":"100xyz"
},
"200xyz":{
"value1": 200,
"value2": 200,
"name":"200xyz"
},
"300xyz":{
"value1": 300,
"value2": 300,
"name":"300xyz"
},
"400xyz":{
"value1": 400,
"value2": 400,
"name":"400xyz"
},
Also see:
- Firebase orderByChild with startAt()'s second argument w/ pagination not odering:
- Limiting the firebase equalTo query with the optional key parameter
- Firebase Query order by and start at name
- How to paging query from Firebase using Android FirebaseUI
这篇关于如何在Android中使用startAt(double value,String key)和endAt(double value,String key)Firebase RealTime Database的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!