I have a method that receives user input and then queries the Cloud Firestore database based on the selected filters. But to achieve the results that I need, I would have to perform a query with multiple "in" operators. Does someone have a workaround?
My method:
getGrouped: async function () {
let array = [];
const groupedQuery = query(
collectionGroup(db, "grouped"),
where("owner", "==", match.params.uid),
where("capaign", "in", this.capaign),
where("region", "in", this.region)
);
const groupedSnapshot = await getDocs(groupedQuery);
groupedSnapshot.forEach((doc: any) => {
array.push(doc.data());
});
return array;
},
The user input looks like this: ["CA", "BO", "TX"]
>Solution :
Firestore only allows one in condition per query. You’ll need to do the second one in JavaScript processing the results.
getGrouped: async function () {
let array = [];
const groupedQuery = query(
collectionGroup(db, "grouped"),
where("owner", "==", match.params.uid),
where("capaign", "in", this.capaign)
);
const groupedSnapshot = await getDocs(groupedQuery);
groupedSnapshot.forEach((doc: any) => {
let data = doc.data();
if (this.region.includes(data.region)) {
array.push(data);
}
});
return array;
},
Caveat: I don’t use Firebase, I’m assuming data.region is the correct way to access a field in the row.