简体   繁体   中英

How to search for more than one value in Firebase

How do I search the firebase for "name" or "middle_name" or "surname" in the same search?

Firebase Database

在此处输入图片说明

//Firebase Search

public void searchPlaces(final String text){
       
       
    Query query = placesRef.orderByChild("name").orderByChild("middle_name").orderByChild("surname")
                   .startAt(text)
                   .endAt(text + "\uf8ff");

           query.addListenerForSingleValueEvent(new  ValueEventListener() {
               
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   for (DataSnapshot ds : dataSnapshot.getChildren()) {
                       list.add(ds.getValue(Places.class));

                   }
               }

               @Override
               public void onCancelled(@NonNull DatabaseError databaseError) {

               }
           });
           }

Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase

In your case however, since you want to search for one value across multiple fields, you will need to perform a separate query for each field.

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