I see that one could use >=
and <
to simulate "starts with" but I do not see a way to perform an "ends with" query using Firestore. Let's say you want to use this to find all email
fields that end with @example.com
. Is that simply not possible?

- 18,696
- 24
- 83
- 112
1 Answers
It is simply not possible. Firestore indexes need to understand the ordering of any given field data so that the indexes can be sharded according to ranges on that ordering. That's why new compound indexes require you to specify ascending or descending order on one of the fields. There is no effortless massive scaling without also knowing the order of the data, and that can't be determined by looking only at the end of the string.
Also note that Firestore only considers the first 1500 bytes of any given field, for the purpose of indexing only, according to the documentation:
Maximum size of an indexed field value - Field values over 1500 bytes are truncated. Queries involving truncated field values may return inconsistent results.
You might want to consider also storing these strings in reverse order in another field, and use that data to make your queries. Or, storing also just the substring of the data that you're interested in for querying.

- 297,357
- 32
- 422
- 441
-
Ah, storing in reverse order -- that is a brilliant idea. Thank you! – Neil C. Obremski Jan 29 '22 at 21:47