3

The Firestore documentation on making queries includes examples where you can filter a collection of documents based on whether some field in the document either matches, is less than, or is greater than some value you pass in. For example:

db.collection("cities").whereField("population", isLessThan: 100000)

This will return every "city" whose "population" is less than 100000. This type of query can be made on fields of type String as well.

db.collection("cities").whereField("name", isGreaterThanOrEqualTo: "San Francisco")

I don't see a method to perform a substring search. For example, this is not available:

db.collection("cities").whereField("name", beginsWith: "San")

I suppose I could add something like this myself using greaterThan and lessThan but I wanted to check first:

  • Why doesn't this functionality exist?

My fear is that it doesn't exist because the performance would be terrible.

proxpero
  • 2,206
  • 1
  • 16
  • 16

2 Answers2

3

[Googler here] You are correct, there are no string operations like beginsWith or contains in Cloud Firestore, you will have to approximate your query using greater than and less than comparisons.

You say "it doesn't exist because the performance would be terrible" and while I won't use those exact words you are right, the reason is performance.

All Cloud Firestore queries must hit an index. This is how we can guarantee that the performance of any query scales with the size of the result set even as the data set grows. We don't currently index string data in a way that would make it easy to service the queries you want.

Full text search operations are one of the top Cloud Firestore feature requests so we're certainly looking into it.

Right now if you want to do full text search or similar operations we recommend integrating with an external service, and we provide some guidance on how to do so: https://firebase.google.com/docs/firestore/solutions/search

Sam Stern
  • 24,624
  • 13
  • 93
  • 124
2

It is possible now:

db.collection('cities')
.where('name', '>=', 'San')
.where('name', '<', 'Sam');

for more details see Firestore query documents startsWith a string

Neil
  • 7,482
  • 6
  • 50
  • 56