4

I would like to perform wildcard queries on a Firebase database, and do not know whether the libraries support this. - (my guess it does not.)

I want an optimal solution where the network traffic will be as little as possible, assume the dataset is millions of records.

Dataset (using prefix wildcard):

user_id:
         id_001123:      
                   name: "java"
         id_002124:
                   name: "objective-c"
         id_003125:
                   name: "swift"

How would the code look like to retrieve the record id_002124 name field using a wildcard, you only have a portion of the id. eg. "%124".

The expected result would be id_002124, and the name being "objective-c"

Any mobile language example code would great. (Objective-C/Swift/Java)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Wayne
  • 3,359
  • 3
  • 30
  • 50

1 Answers1

9

Firebase Database queries can order/filter data based on the object's keys (e.g. id_001123), their values (doesn't apply in your case), or the value of a child property (e.g. the fact that name = "java").

For each of these Firebase can match items that have a specific value you specify, items starting at a value you specify, or items ending at a value you specify.

So you can create a query matching the item named objective-c with:

ref.child("user_id").orderByChild("name").equalTo("objective-c")

Or you can create a query matching id_001124 and id_001125 with:

ref.child("user_id").orderByKey().startAt("id_001125")

Or simply getting all items starting with id_0011:

ref.child("user_id").orderByKey().startAt("id_0011")

Or you can create a query matching id_001123 and id_001124 with:

ref.child("user_id").orderByKey().endAt("id_001124")

Firebase Database cannot filter based on the end of a value. So there is no way in your current data structure to get all items whose key ends in a 4.

Please read more about Firebase Database queries here: https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data

And see some of the many previous questions about searching for FirebaseL

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807