Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore Comparison Operators - contains, does not contain, starts with

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.

like image 292
Stephen Crampton Avatar asked Nov 09 '17 06:11

Stephen Crampton


2 Answers

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")
like image 148
Sam Stern Avatar answered Nov 12 '22 17:11

Sam Stern


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

like image 2
GGAnderson Avatar answered Nov 12 '22 18:11

GGAnderson