Alternative Way to Perform OR Query in Cloud Firestore

Mendhie Emmanuel
The Startup
Published in
2 min readJul 15, 2019

--

If you are using Cloud Firestore for your project’s database, you may have tried performing OR query but soon realized that there’s no function for that. I also encountered the same problem. As a matter of fact, many of us had encountered the same problem and here’s the proof. At the time of writing this article, Google hasn’t provided OR, IN, NOT IN operators for WHERE query.

Therefore, the only approach now, though unpleasant, is to run several queries in a loop and then merge the query results on the client side. I would show you how to achieve this in java. For javascript, use Lim Shang Yi’s approach here.

Cloud Firestore

So here in my database, I have books collection, and each document has three fields- author, genre, and title. In this example, I want to retrieve books from more than one genre. So here we go:

This actually does the job, but it’s not quite efficient because it uses more resources on the client side. I hope Google will provide a more efficient way of doing this soon.

By the way, I used the result to populate this RecyclerView

Books App

…and here’s the source code.

I would love your feedback! Thanks for reading!

--

--