在适用于Android的Firebase数据库上进行查询 [英] Make a query on Firebase database for Android

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

问题描述

我知道有很多类似的问题,但是没有人找到我要的答案,我所咨询的所有问题都有关于childrenEvetListeners的答案...

I know that there are many similar questions but no one has the answer I am looking for, all the questions I consulted have answers talking about childrenEvetListeners...

我的问题要简单得多.这是我的Firebase数据库:

My question is so much simpler. This my Firebase database:

我的问题是:如何进行查询,检查是否存在具有username ="fofof"的用户?

My question is: how can I make a query, checking if a user with the username="fofof" exists?

推荐答案

您拥有混合结构,这使事情变得复杂.但是,如果您只是在/users/-K....下查找匹配项,则可以找到类似以下内容的匹配项:

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:

  • 您将用户存储在推送ID下.虽然推送ID非常适合没有自然键且要按时间顺序存储的集合,但最好将用户存储在UID下.所以/users/s5iu....
  • 您已经将用户信息嵌套了比所需深度更深的一级.在这种情况下,我会摆脱您树中的User级别.
  • 您直接在/users下有一个用户,而其他用户则存储得更深一层.创建这样的混合模型不是一个好主意.
  • 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数据库上进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆