Firebase数据库根据特定键值查询用户数据

时间:2018-02-13 10:42:24

标签: java android firebase firebase-realtime-database

我是FirebaseDatabase的新手, 我在我的Android应用程序中使用以下数据库, 我想收到 isStaff = true的关键值的用户的电子邮件 但我没有得到如何浏览所有用户并找到它!

这是我的数据库

"users": {
        "EPcx2FzeI6U70DPxC3JyExweQvg1": {
            "email": "pizzeriapp101@gmail.com",
            "name": "Admin",
            "phone": "999999999",
            "profile_image": "https://www.livehappy.com/sites/default/files/styles/author/public/default_images/default_profile.png?itok=4-Un3euK",
            "security_level": "1",
            "user_id": "EPcx2FzeI6U70DPxC3JyExweQvg1",
            "isStaff": "true"
        },
        "RjEKX0O0aBZKIBv7XM4TNpTQT403": {
            "email": "kulkarnibharat018@gmail.com",
            "name": "Peter McKinnon",
            "phone": "9986922743",
            "profile_image": "https://firebasestorage.googleapis.com/v0/b/joes-pizzeria.appspot.com/o/images%2Fusers%2FRjEKX0O0aBZKIBv7XM4TNpTQT403%2Fprofile_image?alt=media&token=545f867b-c85c-40ec-b09d-d228da56309d",
            "security_level": "1",
            "user_id": "RjEKX0O0aBZKIBv7XM4TNpTQT403",
            "isStaff": "false"
        },
    "YSCVxPQKXLgfiJFCyNMS1P7BN2I3": {
            "email": "mrblackberryboy@gmail.com",
            "name": "S W",
            "phone": "99632587401",
            "profile_image": "https://firebasestorage.googleapis.com/v0/b/joes-pizzeria.appspot.com/o/images%2Fusers%2FYSCVxPQKXLgfiJFCyNMS1P7BN2I3%2Fprofile_image?alt=media&token=0beb92b5-a33a-478a-8795-7c972810d974",
            "security_level": "1",
            "user_id": "YSCVxPQKXLgfiJFCyNMS1P7BN2I3",
            "isStaff": "false"
        }

请帮忙! 谢谢

1 个答案:

答案 0 :(得分:0)

要解决此问题,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("users").orderByChild("isStaff").equalsTo(true);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String email = ds.child("email").getValue(String.class);
            Log.d("TAG", email);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);