Firebase:根据条件排除数据的查询 [英] Firebase: Query to exclude data based on a condition
问题描述
我可以使用 orderByChild
和 equalTo
(cool它也适用于嵌套的孩子)
private void getData(){
,
try {
final DatabaseReference database = FirebaseDatabase.getInstance()。getReference();
database.child(Constants.TABLE_TASKS).orderByChild(user / id)
.equalTo(somevalue)
.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Timber.d(dataSnapshot.toString());
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
catch(Exception ex){
ex.printStackTrace();
$ b有一个简单的方法来获取数据,其中a没有找到特定的值,基本上像notEqualTo(somevalue)
?解决方案我想我已经找到了解决方案,这更多的是如何设计数据库, Firebase指南背后的意图
https://firebase.google.com/docs/database/android/structure-data
原始设计:
{
child1:{
user:{
id:id1 ,
name:puf
}
},
child2:{
user:{
id:id2 ,
name:abe
}
},
child3:{
user:{$ b $id:id1
name:puf
}
}
}
更新后的设计:
所以除了存储用户的id和名字之外,我们还应该存储一个节点用ID本身作为键,并将其标记为真
{
child1:{
user :{
id:id1,
name:puf
id1:true
}
},
child2:{
user:{
id:id2,
name:abe
id2:true
} $
user:{
id:id1
name:puf
id1 :true
}
}
}
,如果我执行
ref.orderByChild('user / id1')。equalTo(true)
$ b $如果我执行ref.orderByChild(b),我会得到输出为Child1和Child3
'user / id1')。equalTo(null)
我将Child2作为输出
I'm able to get the data for a particular value in the child using
orderByChild
andequalTo
(cool that it works for nested child as well)private void getData() { try { final DatabaseReference database = FirebaseDatabase.getInstance().getReference(); database.child(Constants.TABLE_TASKS).orderByChild("user/id") .equalTo("somevalue") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Timber.d(dataSnapshot.toString()); } @Override public void onCancelled(DatabaseError databaseError) { } }); } catch (Exception ex) { ex.printStackTrace(); } }
Is there an easy way to get the data where a particular value is not found, basically something like a
notEqualTo("somevalue")
?解决方案I think I've found the solution and this is more of how the database should be designed and actually now I understood the intention behind Firebase guideline
https://firebase.google.com/docs/database/android/structure-data
Original Design:
{ child1: { "user": { "id": "id1", "name": "puf" } }, child2: { "user": { "id": "id2", "name": "abe" } }, child3: { "user": { "id": "id1" "name": "puf" } } }
Updated Design:
So apart from the storing the id and name of the user, we should also store a node with the id itself as the key and mark it to true
{ child1: { "user": { "id": "id1", "name": "puf" "id1": true } }, child2: { "user": { "id": "id2", "name": "abe" "id2": true } }, child3: { "user": { "id": "id1" "name": "puf" "id1": true } } }
With the updated design, if i execute
ref.orderByChild('user/id1').equalTo(true)
I would get output as Child1 and Child 3
and if i execute
ref.orderByChild('user/id1').equalTo(null)
,I would get Child2 as the output
这篇关于Firebase:根据条件排除数据的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!