abdullahislam_A
Convex Community2y ago
2 replies
abdullahislam_

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(); 
Was this page helpful?