15

From the documentation "...The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, or >=..."

The queries that we need to carry out are:

  • equals (==)
  • not equal to (???)
  • less than (<)
  • greater than (>)
  • less than or equal (<=)
  • greater than or equal (>=)
  • contains (???)
  • does not contain (???)
  • starts with (???)

In this question the suggestion is to implement a full text search such as Elastic or Algolia. I don't need full text search - I just need these basic operators to search in nominated fields. But the bigger problem I have is that my app is off-line for substantial periods of time and we cache the data we need off-line and off-line full text search is not an option (except if you get the Enterprise ($$$$$$) license of Algolia - but still seems like overkill for what we are looking for).

Do any of you have any solutions for where("FIELD", "???", "string") when "???" is "not equal to", "contains", "does not contain", or "starts with"?

Any ideas gratefully appreciated.

  • 3
    I thinks google should come with a solution for this conatain saga – suulisin Nov 24 '17 at 10:16
  • Good points, and where is the OR operator in queries. You can only do AND? – AdamG Nov 24 '17 at 18:55
  • 5
    In one of the comments from Google/Firebase they state that they are working on these sort of things. What would be really helpful is if someone from Google / Firebase could give an indicative time line. They say its a high priority and I understand Firestore is still in Beta. But a non-committal "we are hopeful" of having it in 3 months or 3 years would be really helpful. If its 3 months then maybe we limp along as is, or 3 years we need to look for alternate solutions for offline search in Firestore. – Stephen Crampton Nov 24 '17 at 21:09
  • @StephenCrampton clever use of the "array-contains" query operator as described in my answer below is worth considering. – GGAnderson Apr 16 '19 at 17:52

2 Answers2

14

There are no native "contains", "does not contain", "starts with" or "ends with" queries in Cloud Firestore.

You can approximate a very limited "starts with" query using < and >, however:

// All names starting with "Sa"
db.collection("people")
  .where("name", ">", "Sa")
  .where("name", "<", "Saz")
Sam Stern
  • 24,624
  • 13
  • 93
  • 124
2

Firestore has added the "array-contains" query operator. This now means that we can store a deconvoluted version of the field as an array of sub-strings, then use the "array-contains" operator. See:

https://github.com/googleapis/nodejs-firestore/releases/tag/v0.16.0

GGAnderson
  • 1,993
  • 1
  • 14
  • 25