本文介绍了在Android版Firebase数据库上进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的问题,但没有人有我正在寻找的答案,所有我咨询的问题都有答案,我的问题是如此简单。这是我的Firebase数据库:


谢谢!!

解决方案 / users / -K .... 下查找匹配项,您可以找到类似的匹配项:

  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());
}
});

有关您当前设置的警告:


  • 您正在将用户存储在推送ID下。虽然推送ID对于没有自然键且您想要按时间顺序存储的集合非常适合,但最好将用户存储在UID下。所以 / 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数据库上进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!