Himanshu Arora Himanshu Arora - 5 months ago 41
Android Question

How to perform search in a FirebaseDatabase?

Description



My Firebase NoSQL Database should look somewhat like this:

Users

A user Java model should get pushed whenever a new user signs in.

Problem



How to check if the user with the email address already has an account? Since the email address is at: Root > PushID > email; and push ID is automatically generated, I am not sure on how to iterate on all push ID's and check if any of them has its email key set to the specified value.

I have read the documentation but unable to figure out how to solve this problem.

Learning from Shubhank's answer



Firebase Methods which involve a child can be applied to any of its children irrespective of its level in the hierarchy i.e. not limited to immediate children.

In this problem, the child to be searched was 2 levels below the common parent. The first level child can be ignored and query methods can be directly applied on the target child.

Answer

You should first make a model class for the user

class User { 
    public String email;
    public String name;
}

Then get the users from the db and map them into User objects

  1. Simple loop through all results query

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    
    // i don't know the end point since you have not specified it in the image           
    database.getReference("myUsersEndPoint").addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                      for (DataSnapshot data : dataSnapshot.getChildren()) {
                          User user = data.getValue(User.class);
                          if (myField.getText().toString().eqaulsIgnoreCase(user.email)) {
                            // exist
                          }
                      }
    
                   }
    
          @Override
          public void onCancelled(DatabaseError databaseError) 
                    Log.w("MyApp", "getUser:onCancelled", databaseError.toException());
          }
       });
    
  2. Searching only for specific Values In this example, we use the orderByChild and equalTo method to limit result to specific child value

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    
    // i don't know the end point since you have not specified it in the image           
    database.getReference("myUsersEndPoint").orderByChild("email").equalTo("emailToSearchhere").addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                      for (DataSnapshot data : dataSnapshot.getChildren()) {
                          // here the user will have the specified email only 
                      }
    
                   }
    
          @Override
          public void onCancelled(DatabaseError databaseError) 
                    Log.w("MyApp", "getUser:onCancelled", databaseError.toException());
          }
       });
    
Comments