2

My data structure looks like:

Database
 Data1 { 
   Family: "Adam, John, Smith, Bob, Alice"
   Age: "12, 23, 6, 5, 8"
   Area: "GT6, GT9, GT12"
}

 Data2 { 
   Family: "Tom, Ben, Berry, Hosh, Nico"
   Age: "68, 43, 26, 15, 18"
   Area: "67D, HG6, DHJD, 453"
}
 Data3 { 
   Family: "Alice, Karan, faser, jenny, kietty"
   Age: "14, 10, 15, 17, 5"
   Area: "HG6, 67D, DHJD"
}

I want to only print / retrieve data that contains for example Area "67D", in this I want to print all data from data 2 and 3.

Please let me know if my question is not clear.

I have tried

Query queryRef = ref.orderByChild("Area").equalTo("67D");
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
        System.out.println(snapshot.getKey());
    }
    // ....
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SumOne
  • 817
  • 3
  • 13
  • 24
  • 1
    The Firebase database has no equivalent of a SQL `LIKE` operation. If you want to associate an item with multiple values, you're essentially trying to create a categorization system. See my answer here for a more scalable solution: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Feb 02 '17 at 14:09

2 Answers2

1

According to this SO answer by Kato, and this Firebase blog post, it isn't possible to filter by substring matching.

Consider having a child with the areas as keys instead, and then your query will work:

ref.orderByChild("Area").equalTo("67D");
Community
  • 1
  • 1
orip
  • 73,323
  • 21
  • 116
  • 148
0

You can iterate trough all your Data and find the matching Area by checking if each Area contains the String you want. Considering ref is your Database Referece :

ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    DataSnapshot area = dataSnapshot.child("Area");
    String area_value = area.getValue().toString();
    if(area_value.contains("67D")){
        System.out.println(dataSnapshot.toString());
    }
}
Hristo Stoyanov
  • 1,960
  • 1
  • 12
  • 24