Home > Back-end >  How to search Firebase database by any field?
How to search Firebase database by any field?

Time:11-12

Is there any way I could search by any Reatime Firebase Database field? Right now I know how to search by FP_CarNumber, but I would like to search by others as well like SP_CarNumber or SP_CarNumber and FP_CarNumber at the same time.

    private void txtSearch(String str){
        FirebaseRecyclerOptions<MainModel> options =
                new FirebaseRecyclerOptions.Builder<MainModel>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("Declaration_Data").child(currentuser).child("Declarations").orderByChild("FP_CarNumber").startAt(str).endAt(str   "\uf8ff"), MainModel.class)
                        .build();

        mainAdapter = new MainAdapter(options);
        mainAdapter.startListening();
        recyclerView.setAdapter(mainAdapter);

    }

CodePudding user response:

The Realtime Database only supports queries on single field property. I have explained in one o my answers:

How to solve such a situation by creating an extra field to keep the other values. Unfortunately, that workaround doesn't apply to all situations.

Besides that, as far as I know, there is no library that does that automatically in Android. However, there is one for the web, written by David East called Querybase.

So unlike in the Realtime Database, Cloud Firestore allows compound queries. So I recommend you try that.

  • Related