cameronm
cameronm2w ago

IN operator equivalent?

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();
}
});
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();
}
});
3 Replies
Convex Bot
Convex Bot2w ago
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 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!
erquhart
erquhart2w ago
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();
},
});
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();
},
});
cameronm
cameronmOP2w ago
Cool, thanks for the quick response!

Did you find this page helpful?