abdullahislam_
abdullahislam_•6mo ago

Filter scanning entire table - any alternative to withIndex?

Hi - so I noticed one of my functions is using up a LOT of bandwith on reads, and I think this code is the culprit. The thing is I need to use an OR statement to search for users where either the email address matches OR the anonId matches. I don't think I can do this using withIndex, cos it doesn't support u.or() The only alternative to this I can think of is to run 2 seperate queries: 1. Run a query for just email on using a withIndex("by_email") 2. Run another query for anonId using a withIndex("by_anonId") Is that the best approach? RIP my bandwidth for this month 😦

const userRecord = await ctx.db
.query("users")
.filter((u) =>
u.or(
u.eq(u.field("email"), args.email),
u.and(
args.anonId != null && args.anonId != "",
u.eq(u.field("anonId"), args.anonId)
)
)
)
.first();

const userRecord = await ctx.db
.query("users")
.filter((u) =>
u.or(
u.eq(u.field("email"), args.email),
u.and(
args.anonId != null && args.anonId != "",
u.eq(u.field("anonId"), args.anonId)
)
)
)
.first();
2 Replies
abdullahislam_
abdullahislam_OP•6mo ago
Update: This seems to be working wonders for me: Run a query for just email on using a withIndex("by_email") Run another query for anonId using a withIndex("by_anonId")
lee
lee•6mo ago
you found the solution we would have suggested 🙂

Did you find this page helpful?