8

Suppose I have a simple firebase realtime database structure where the key is username and the value is userid. Now I want to search userid by username. It is quite easy if the username matches. but how to get the answer for partial match. To be more clear if i were to write this using SQL it would be something like:

SELECT userid FROM tablename WHERE username LIKE 'abc%'

it will give me all the userid where the username starts with abc. How to write this type of query for firebase? Understand i am using this on a android device and there can be a lot of user.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
NPN
  • 125
  • 1
  • 8

1 Answers1

10

According to the doc of Java Admin you can do something like this it should also work with Android

For data starting with abc,

yourRef.orderByKey().startAt("abc").endAt("abc\uf8ff")

The \uf8ff is after most regular characters in Unicode, so the query should matches all values that start with a abc.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
  • Yes it works. Thanks. But can you explain "\uf8ff" a little more. Also will it return a array of userid ? – NPN Sep 28 '17 at 05:53
  • As I've explain that `\uf8ff is after most regular characters in Unicode` and It will return `Query` object in Firebase you can get data by `attaching` `ValueEventListener`. Then get data from datasnapshot. – Boonya Kitpitak Sep 28 '17 at 08:34