How to create the firestore query with a list of conditions:
function createQuery(conditions){
// if conditions is = [
// ["state", "==", "CO"],
// ["name", "==", "Denver"]
// ]
return query; // will return query(citiesRef, where("state", "==", "CO"), where("name", "==", "Denver"));
}
Answer
See answer: Firestore conditional where clause using Modular SDK v9
let q = query(collection(firestore, "articles"))
// filters selected by users
if (status) q = query(q, where("status", "==", "live"))
if (publishedAfter) q = query(q, where("publishedAt", ">", publishedAfter))
From the documentation on compound queries, to have multiple conditions in query, we can do this:
const q1 = query(citiesRef, where("state", "==", "CO"), where("name", "==", "Denver"));
And if I have 3 conditions, we can do this:
const q1 = query(citiesRef, where("state", "==", "CO"), where("name", "==", "Denver"), where("age", "==", 30));
How can we dynamically add where into query such that given:
let conditions = [
["state", "==", "CO"],
["name", "==", "Denver"]
]
will return:
const q1 = query(citiesRef, where("state", "==", "CO"), where("name", "==", "Denver"));
And
let conditions = [
["state", "==", "CO"],
["name", "==", "Denver"],
["age", "==", 30]
]
will return:
const q1 = query(citiesRef, where("state", "==", "CO"), where("name", "==", "Denver"), where("age", "==", 30));