1

I'm trying to create dynamic queries to implement filtering feature in my app. I'm using Cloud Firestore v9 as a backend, and I don't know how to create a query that depends of filters selected by the client (Angular). I have created other backends using Java and that language allows you to create a dynamic sql queries using CriteriaBuilder or you can simply use an "if else" structure to hardcode the wanted query as string. The problem is when I try to create a query using the library of Firebase, there aren't methods to allow me to create the dynamic query, and the unique solution is to create an specific query per each possibility. For example: Imagine that I have 3 filters for filtering users collection, for example, name, location, and age. If I want to implement that filter I have to create one query per each combination of filters:

q1 = query(where(name,==,criteria.name))
q2 = query(where(name,==,criteria.location))
q3 = query((where,name,==,criteria.age))
q4 = query((where,name,==,criteria.name),where(name,==,criteria.location))
q5 = query((where,name,==,criteria.name),where(name,==,criteria.age))

......

In that example with only 3 conditions I have to create 7 different queries, imagine If I have 10 filters or more ...

Please Help me I don't want to spend 10 hours hardcoding all the possibilities

1 Answers1

0

With Firestore as mentioned here you can use the in clause if you want to filter based on the same field multiple times. Also you can use multiple where clauses as mentioned here to filter based on different fields.

But currently there is a limitation of 10 logical OR operators in a Firestore query. So inside the in clause you can use a maximum of 10 values.

Prabir
  • 1,415
  • 4
  • 10