Firebase – searching where string starts with

Firebase doesn’t have a nice way to say .where(searchField, ‘startsWith’, searchTerm)

The easiest way is to search for text strings that are greater than the search term, and smaller than the next string after the search term.

E.g. when searching for rows that start with “Rhys”, we really want everything greater or equal than Rhys, and everything less than or equal to RhysZ (to oversimplify).

Here’s a quick and easy way to do that, using \uF8FF (a high unicode code point) as the trailing character.

 return usersCollection
  .where('email', '>=', searchText)
  .where('email', '<=', searchText + '\uF8FF')