0

I'm trying to make a firestore query dynamically/programmatically based on user input. The problem is the input could be a variable amount unknown at the time of programming. I want to create the query in such a way that it will run if the user enters one input or many search terms.

My example code is in Kotlin for Android:

val searchFilters = listOf(
    "searchField1",
    "searchField2",
    "searchField3"
)
var refQuery= db.collection("myCollection")
for(filterName in searchFilters) {
  refQuery.whereEqualTo(filterName, true)
}
refQuery.get()

The above code doesn't appear to actually add any filter options and the entire collection is returned. What is the correct way to dynamically or programmatically create a firestore query?

EDIT: The answer can be found here, but I'll leave this here for other's search reference as the way it is asked is worded differently enough to warrant it.

Have to reassign it to itself:

for(filterName in searchFilters) {
  refQuery = refQuery.whereEqualTo(filterName, true)
}
Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • 2
    You have to reassign `refQuery` each time you add a conditional filter to it. Query objects are immutable. – Doug Stevenson Nov 18 '20 at 05:07
  • Thanks. I'll leave the edit here for reference as the way the question is asked might lead others here who want to know the same thing. – Mr.Drew Nov 18 '20 at 05:18

0 Answers0