noob saibot
noob saibot11mo ago

How can I run the equivalent of this

How can I run the equivalent of this query select ... where field in (list of values)? It looks like there is no in operator. Is the only way to build a long list of or filters?
2 Replies
oferitz
oferitz11mo ago
Not sure if you have the same use case but maybe you will find this helpful: https://discord.com/channels/1019350475847499849/1019350478817079338/1220796223666257980
sshader
sshader11mo ago
There's no efficient way to do this in a single db.query (check out https://stack.convex.dev/databases-are-spreadsheets#no-efficient-in--or hot off the presses). You can either do a separate db.query for each thing in your list, or scan the full table and then apply the filter (either with a .filter with a bunch of ors, or in JavaScript, both are effectively the same performance, check out https://stack.convex.dev/complex-filters-in-convex for a deeper dive). So something like
Promise.all(values.map(v => db.query("myTable").withIndex("ByField", q => q.eq("field", v))))
Promise.all(values.map(v => db.query("myTable").withIndex("ByField", q => q.eq("field", v))))
or
const allDocs = await db.query("myTable").collect();
const filteredDocs = allDocs.filter(d => values.includes(d.field))
const allDocs = await db.query("myTable").collect();
const filteredDocs = allDocs.filter(d => values.includes(d.field))
Using TypeScript to Write Complex Query Filters
There’s a new Convex helper to perform generic TypeScript filters, with the same performance as built-in Convex filters, and unlimited potential.
Databases are Spreadsheets
I want to share my mental model of databases: - Databases are just big spreadsheets - An index is just a view of the spreadsheet sorted by one or mor...

Did you find this page helpful?