我还是Firebase的新手,还在学习如何做"简单"有它的东西。我的结构看起来像这样:
"users": {
"-KY5FJBudXmKv07noYbZ": {
"emailAddress" : "a@a.com",
"tags": {
"red": "8dfhj34sd983ie",
"green": "9f5d6g8er2d"
}
},
"-KY5JM-NGjNgUXwrBRhs": {
"emailAddress": "b@b.com",
"tags": {
"blue": "58ewgsg656fd",
"pink": "sdf6g3dg5e6d"
}
},
"-K_0c7wslBiOceF30R-5": {
"emailAddress": "c@c.com",
"tags": {
"blue": "58ewgsg656fd",
"brown": "xx4f5g68d5d1",
"black": "8941gs8536sdf1"
}
},
}
我想要做的是获取users
中包含tags
中儿童的节点,其值为58ewgsg656fd
。
在此示例中,这将是"blue": "58ewgsg656fd"
子节点,并且包含它们的users
节点是该结构中的最后两个节点(...0R-5
和...BRhs
)。< / p>
这是我到目前为止所做的:
DatabaseReference usersRef = database.getReference("users");
usersRef.orderByChild("tags").[NOT SURE WHAT GOES HERE].addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() == 0) {
// No users found with that tag
} else {
// Users found with that tag
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Error occurred
}
});
我应该编写什么代码来获取这些节点?
答案 0 :(得分:1)
ArrayList<String> user=new ArrayList<>();
DatabaseReference usersRef=FirebaseDatabse.getInstance().getReference();
usersRef.child("users").addValueEventListener(new ValueEventListener()
{
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot>iterator=dataSnapshot.getChildren().iterator();
while(iterator.hasNext())
{
DataSnapshot snapshot=iterator.next();
if(snapshot.child("tags").child("58ewgsg656fd").exists())
{
user.add(snapshot.getKey());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});