简体   繁体   中英

Query from Firebase dB

I am new to Firebase and would like to know how to retrieve all the entries, given one specific value, like names . My db is like this:

{
  "-L6wXAM4Yt6OeUx6kLM9" : {
    "date" : "Tue Mar 06 14:17:53 GMT-04:00 2018",
    "names" : "Raamon"
  },
  "-L6wXAM84ti-Ea0vzKfx" : {
    "date" : "Tue Mar 06 14:17:53 GMT-04:00 2018",
    "names" : "Anne"
  },
  "-L6whnSSZXHxdjPvGgKI" : {
    "date" : "Tue Mar 06 15:08:41 GMT-04:00 2018",
    "names" : "Anne"
  }
}

My rules:

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null"
  }
}

Should i use the Query function? Or something else?

Try this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query q = ref.orderByChild("names").equalTo(Raamon);
q.addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String dates=datas.child("data").getValue().toString();
     }
   }   
   @Override 
 public void onCancelled(DatabaseError databaseError) {
      }
  });

using one specific value name that is equal to Raamon for example, you can then retrieve the date that is related to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM