如何在不知道父键的情况下按特定值查询firebase数据库

时间:2016-12-07 10:55:03

标签: android firebase firebase-realtime-database

enter image description here

我想找到密钥(例如:-KYNc6XTtavDtsC-VoqIb),其中的电子邮件是 amitabh@gmail.com
我尝试了很多代码,但没有工作,我得到了完整的数据列表 这是我的代码:

database = FirebaseDatabase.getInstance();
myRef = database.getReference("members");
myRef.orderByChild("email").startAt(emailId).endAt(emailId);

这就是我所得到的。

DataSnapshot{
  key=members,
  value={
    -KYNYiw5B1d8Brdz5KdR={
      lastname=zxvzxvfsdf44,
      email=fasdfds@rr44,
      firstname=fdafaf44
    },
    -KYNYdX_gZ0tU-AbI1Ab={
      lastname=zxvzxvfsdf,
      email=fasdfds@rr,
      firstname=fdafaf
    },
    -KYNd02QZptRktmR02IH={
      lastname=dfasf,
      email=you@bo.com,
      firstname=amita
    },
    -KYNdlMM4zzlUT9FdSdu={
      lastname=99dfasfs,
      email=99@bos.com,
      firstname=99
    },
    -KYNdBPMqxgSzm1ai92B={
      lastname=dfasfs,
      email=you@bos.com,
      firstname=amitas
    },
    -KYNlYF4OzzCCewJuatt={
      lastname=go2o,
      email=go@to.to,
      firstname=go
    },
    -KYNo7ZvLQeCGm97yUvf={
      lastname=bamio,
      email=samio@ss.com,
      firstname=amio
    },
    -KYNdBRAipnlTYCCUwz6={
      lastname=dfasfs,
      email=you@bos.com,
      firstname=amitas
    },
    -KYNc6XTtavDtsC-VoqI={
      lastname=Sarkar,
      email=amitabh@gmail.com,
      firstname=amitabh
    },
    -KYNcHdlZjUlNSsjKlyW={
      lastname=gjaldga,
      email=ee@ee.com,
      firstname=ami
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

myRef.orderByChild("email").startAt(emailId).endAt(emailId).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        Log.e("onChildAdded", dataSnapshot.toString());
    }
});