Reputation: 53
project name > users > user1key > name , latitude , longitude
user2key > name , latitude , longitude
This is the tree in database. I want a query to search the user using its name. and get its details.
Upvotes: 1
Views: 3253
Reputation: 598797
Something like this should do the trick:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users");
ref.orderByChild("name").equalTo("Yash Mehta").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("Name").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Upvotes: 3
Reputation: 1812
you are looking for something like the following.
This is example search name
refDatabase.child("users").orderByChild("user2key").orderByChild("name").equalTo("search here word").addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//data will be available on dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
If it does not meet the requirements try these
How to search for a value in firebase Android
Upvotes: 0