Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple "in" filters in Firestore query

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading