0

I was watching this Firebase video, and one stuff that wasn't clear to me is that the "||" or OR operator isn't supported especially with the way Firebase store indexes. It was stated that you have to make separate queries and join it on the client side instead of on the firebase side.

Isn't the in operator essentially just a convenience method and acts like multiple OR statements? https://firebase.blog/posts/2019/11/cloud-firestore-now-supports-in-queries

The.Wolfgang.Grimmer
  • 1,172
  • 2
  • 9
  • 32
  • Citation from the page you linked: "*in queries are a good way to run simple OR queries in Cloud Firestore.*" So yes, in a very specifc scenario `IN` is sort of an OR operation. – derpirscher Jan 21 '23 at 08:01
  • Yeah I got that part. However, the part that isn't clear to me was related to way OR operations are NOT allowed due to indexes structure in firebase (if we're basing on the linked firebase video alone). – The.Wolfgang.Grimmer Jan 21 '23 at 08:04
  • If you really want the details how the IN is implemented in Firebase, you probably would need to look into the source code. Maybe it just moves the process of executing multipe queries and merging the results to the backend. The limitation of 10 elements might indicate something like this ... – derpirscher Jan 21 '23 at 08:26
  • @The.Wolfgang.Grimmer,let me know if below information was helpful. – Vaidehi Jamankar Jan 25 '23 at 09:42

1 Answers1

0

It is a well known feature of Firestore indexes which are good for range queries. With the indexes applied , for an inequality query, the backend would still have to scan every document in the collection in order to come up with results, and thus will affect the performance when the number of documents increases with time.
So, as per your question regarding the backend logical working of the “in” operator when used in a query and which is also mentioned in this thread on addition of IN queries not only address this performance issue but also supports up to 10 equality clauses on the same field with a logical OR".The arguments which are passed in the “In” operator query, are compared when searching a document.This will allow you to fetch documents with your filter criteria and thus result in function operation to take less time rather than goind one by one through each item.

For the example you could do:

// Get all documents in 'foo' where status is open or upcoming  
 db.collection('foo').where('status','in',['open','upcoming']).get()

I would also recommend you to check these following similar examples:

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10