Home > other >  Multiple "in" filters in Firestore query
Multiple "in" filters in Firestore query

Time:12-29

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"]

CodePudding user response:

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) => {
      if (this.region.includes(doc.get('region'))) {
        array.push(doc.data());
      }
    });
    return array;
  },    
  • Related