0

I need to retrieve the details where the reportedAt = "moka" && name = "chickungunya". as displayed in the firebase screenshot attached

Please help

 //Filter markers by disease
disease = String.valueOf(spnDisease.getSelectedItem()).toLowerCase();
location = String.valueOf(spnLocation.getSelectedItem()).toLowerCase();

Query dbQuery = FirebaseDatabase.getInstance().getReference().child("diseaseReported").orderByChild("name").equalTo(disease);
dbQuery.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Log.d("v1", dataSnapshot.toString());
        Map m = dataSnapshot.getValue(Map.class);
        double lat = m.getLat();
        double lng = m.getLng();
        String reportedAt = m.getReportedAt();
        String reportedOn = m.getReportedOn();
        String name = m.getName();
        String snippetText = "Reported on: " + reportedOn;
        LatLng c = new LatLng(lat, lng);

        mMap.addMarker(new MarkerOptions().position(c).title("Disease reported: " + name).snippet(snippetText).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

        }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Firebase screenshot

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Feb 13 '18 at 17:56

2 Answers2

1

In firebase you can't query two separate pieces of information for data. You'll have to create another key in your database titled: "name_reportedAt" that contains the data "chickungunya_moka". Then you can query on that directly.

Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
1

These type of queries are not allowed in firebase realtime database You can use firestore for multiple where queries by the way.

If you want to stick with firebase realtime database then you should work on your data model and create combine key to search and perform AND operation.

For example to perform AND operation you need to create a key

"name_createdAt" = "chickungunya_moka"

and then do query for

orderByChild("name_reportedAt").equalsTo(name+"_"+reportedAt);

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42