MathiasM
Convex Community2y ago
2 replies
Mathias

How to query and filter with multiple arguments?

I'm trying to query workspaceMembers, where both userId and workspaceId are true. I tried below but it returned null:

const getWorkspaceMemberItem = await ctx.db
      .query("workspaceMembers")
      .filter((q) =>
        q.and(
          q.eq("userId", args.userId),
          q.eq("workspaceId", args.workspaceId)
        )
      )
      .first()


I also tried with withIndex but it seems you can only use one. What is a good way to query with multiple arguments for filtering or indexing?

Update:
I got the following to work for my use case, but I'm still not sure if that is the most efficient or correct way to do it:

const workspaceMembers = await ctx.db
      .query("workspaceMembers")
      .withIndex("by_userId", (q) => q.eq("userId", args.userId))
      .collect()

    const getWorkspaceMemberItem = workspaceMembers.find(
      (member) => member.workspaceId === args.workspaceId
    )
Was this page helpful?