问题描述
我知道有很多类似的问题,但没有人有我正在寻找的答案,所有我咨询的问题都有答案,我的问题是如此简单。这是我的Firebase数据库: 谢谢!!
Query query = ref.child(users)。orderByChild(User / username)。equalTo(fofof);
(DataSnapshot userSnapshot:dataSnapshot.getChildren())
query.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
b Log.i(TAG,fofof存在并存储在键+ userSnapshot.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError){
//获取发布失败,记录消息
Log.w(TAG,loadPost:onCancelled,databaseError.toException());
}
});
有关您当前设置的警告:
/ users / s5iu ...
。
用户
级别。
/ users
,而另一些存储在更深的一层。创建这样一个混合模型是一个坏主意。
I know that there are many similiar questions but no one has the answer I am looking for, all the questions I consulted have answers talking about childrenEvetListeners...
My question is so much simpler. This my Firebase database:
My question is: how can I make a query, checking if a user with the username="fofof" exists?
Thank you!!
You have a hybrid structure, which complicates things. But if you are just looking for a match under /users/-K....
, you can find the match with something like:
Query query = ref.child("users").orderByChild("User/username").equalTo("fofof");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
Log.i(TAG, "fofof exists and is stored under key "+userSnapshot.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
A few warnings about your current set-up though:
- you're storing users under a push ID. While push IDs are great for collections that don't have a natural key and that you want to store chronologically, it is better to store your users under the UID. So e.g.
/users/s5iu...
. - you've nested the user information one level deeper than is necessary. While this works, I'd get rid of the
User
level in your tree. - you have a user directly under
/users
, while others are stored one level deeper. Creating such a hybrid model is a bad idea.
这篇关于在Android版Firebase数据库上进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!