#IN operator equivalent?

1 messages · Page 1 of 1 (latest)

tender crow
#

Is there a way to do an IN operation when querying from an array of values? What's the most efficient/recommended way?

This is what I am currently doing, but just wondering if there is a better way

export const getReports = query({
    args: {
        date: v.string(),
        regionIds: v.array(v.id('regions')),
        propertyTypeIds: v.array(v.id('propertyTypes'))
    },
    handler: async (ctx, args) => {
        // Generate all combinations and query each using the full index
        const allQueries = [];

        for (const regionId of args.regionIds) {
            for (const propertyTypeId of args.propertyTypeIds) {
                allQueries.push(
                    ctx.db
                        .query('reportsResidentialSales')
                        .withIndex('by_date_region_propertyType', (q) =>
                            q.eq('date', args.date).eq('regionId', regionId).eq('propertyTypeId', propertyTypeId)
                        )
                        .collect()
                );
            }
        }

        // Execute all queries in parallel
        const results = await Promise.all(allQueries);

        // Flatten the results
        return results.flat();
    }
});
clever mantleBOT
#

Thanks for posting in #1088161997662724167.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.

    - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
    - Use [search.convex.dev](https://search.convex.dev) to search Docs, Stack, and Discord all at once.
    - Additionally, you can post your questions in the Convex Community's #1228095053885476985 channel to receive a response from AI.
    - Avoid tagging staff unless specifically instructed.

    Thank you!
spark oasis
#

You're doing it right. You can also async map it, but the perf and everything would be the same.

export const getReports = query({
  args: {
    date: v.string(),
    regionIds: v.array(v.id("regions")),
    propertyTypeIds: v.array(v.id("propertyTypes")),
  },
  handler: async (ctx, args) => {
    // Generate all combinations and query each using the full index
    const results = await asyncMap(args.regionIds, (regionId) => {
      return asyncMap(args.propertyTypeIds, (propertyTypeId) => {
        return ctx.db
          .query("reportsResidentialSales")
          .withIndex("by_date_region_propertyType", (q) =>
            q
              .eq("date", args.date)
              .eq("regionId", regionId)
              .eq("propertyTypeId", propertyTypeId)
          )
          .collect();
      });
    });
    return results.flat();
  },
});