How to use multiple orederbychild in Firebase database?

Hi everyone, I am using firebase base in my application. I can add data to the database. Now I want to implement a search in my application, I have two options for search users,

1) Blood type
2) User area

I can get blood type data, but I don’t know how I can get blood type and region data. (Filter with multiple filters)

Now, if the user selects 'A +' as the blood type from the counter and select Area 'ABC', then the result will look like users with the blood type β€œA +” and β€œABC”.

 search_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Default Selected"+sel_blood_group); mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { //Your Logic here for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) { UserRegisterModel mModel = eventSnapshot.getValue(UserRegisterModel.class); // Log.e("DATA" ,""+ mModel.getName()); } Query chatRoomsQuery = mFirebaseDatabase.orderByChild("blood_group").equalTo(sel_blood_group); chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { // dataSnapshot is the "issue" node with all children with id 0 for (DataSnapshot issue : dataSnapshot.getChildren()) { // do something with the individual "issues" UserRegisterModel mModel = issue.getValue(UserRegisterModel.class); Log.e("QUERY DATA" ,""+ mModel.getName()); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); Log.e("DATA" ,""+ chatRoomsQuery.toString()); } @Override public void onCancelled(DatabaseError databaseError) { } }); } }); 
-one
android firebase firebase-database
May 2 '17 at 8:24
source share
2 answers

The Firebase RealTime database does not support multiple where clauses. Therefore, try executing the query with one filter, and then filter the next one programmatically.

 search_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Default Selected"+sel_blood_group); System.out.println("Default Selected"+sel_area); mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { //Your Logic here for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) { UserRegisterModel mModel = eventSnapshot.getValue(UserRegisterModel.class); // Log.e("DATA" ,""+ mModel.getName()); } Query chatRoomsQuery = mFirebaseDatabase.orderByChild("blood_group").equalTo(sel_blood_group); chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { // dataSnapshot is the "issue" node with all children with id 0 for (DataSnapshot issue : dataSnapshot.getChildren()) { // do something with the individual "issues" UserRegisterModel mModel = issue.getValue(UserRegisterModel.class); if(mModel.getArea().equals(sel_area)) Log.e("QUERY DATA" ,""+ mModel.getName()); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); Log.e("DATA" ,""+ chatRoomsQuery.toString()); } @Override public void onCancelled(DatabaseError databaseError) { } }); } });enter code here 
+1
May 02 '17 at 10:08 a.m.
source share

You can make userName added only once using the following method. I assumed that you are storing user data in the UserRegisterModel class. Please take care of DatabaseReferences as I used what you mentioned and I do not know about your database structure.

 Query chatRoomsQuery = mFirebaseDatabase.orderByChild("User_Name").equalTo(userRegisterModel.getUserName()); chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { Log.e("UserAlready Registered"); } else { mFirebaseDatabase.push().setValue(userRegisterModel);; Log.i("User added Successfully"); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); 

I have not tested the code, but I think it will work! :)

0
May 03 '17 at 7:16 a.m.
source share



All Articles