3

I have below firebase structure. From this I need to fetch data based on query where Dstatus=ok and Drmoble=mobile no

enter image description here

so far I have written this which is working for a single value only.How can I combine both

 mReferenceBooks.orderByChild("Drmoble").equalTo(mobile_no).addValueEventListener(new ValueEventListener() {

 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mithu
  • 665
  • 1
  • 8
  • 38
  • 1
    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: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Dec 05 '19 at 14:21

1 Answers1

8

In firebase realtime database, their is no AND query, which means you can't do:

mReferenceBooks.orderByChild("Drmoble").equalTo(mobile_no).orderByChild("DStatus").equalTo("ok")

To solve this you can create another field that will contain both the DStatus and the Drmoble, for example:

randomId
     dstatus-drmoble : "01810000-ok"

Then you can use the following query:

mReferenceBooks.orderByChild("dstatus-drmoble").equalTo("01810000-ok").addValueEventListener(new ValueEventListener() {
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134