How to query an array in filter or withIndex
I want to query an array of users by either filter or withindex, any assistance will be great.
Example of schema -
chat: defineTable({
isGroupChat: v.boolean(),
participants: v.array(v.id("users")),
chatContext: v.optional(v.string()),
})
.index("participants", ["participants"])
Want to filter out by the userID inside of the participants... please don't say I need to fetch the entire chat and then filter it out.
4 Replies
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!
Check out https://www.convex.dev/can-do/relational-data
btw, you asked for "filter or withIndex" but then specified you don't want to fetch the entire thing and filter it out. So you don't want to use "filter", you need "withIndex". Check out https://stack.convex.dev/complex-filters-in-convex#array-includes-with-index-and-a-join which explores this exact array-containment query
Relational Data: Convex can do that
Represent & navigate relationships between db tables
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.
Thank you for the guides, I believe it contains what I'm looking for. Very new to convex, apologies for confusing question/statement.
For sure. We have lots of resources on this because it's a common question. It's not obvious how you can build complex queries in convex, since it's different from most other databases