#Query where a list includes

1 messages · Page 1 of 1 (latest)

kind flax
#

I want to write a query where a list includes a user id

dapper oarBOT
#

Hi @kind flax. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the knowledge sources provided, Convex does not directly support a contains or includes operator for querying lists. However, you can achieve this by fetching the entire list and then filtering it in your application code.

Here's an example of how you might do this:

export default query(async (ctx) => 
  (await ctx.db.query("messages").collect())
    .filter(m => m.content.includes('userId'))
)

In this example, messages is the name of the collection you're querying, content is the field that contains the list, and 'userId' is the user ID you're looking for. Replace these with your actual collection name, field name, and user ID.

Please note that this approach fetches the entire collection and then filters it in your application, which might not be efficient for large collections. For more efficient querying, you might want to consider structuring your data in a way that allows you to use Convex's built-in query operators.

For more information, you can refer to the Convex documentation (https://docs.convex.dev/api/interfaces/server.FilterBuilder) and this Discord support thread (https://discord.com/channels/1019350475847499849/1266472048369336351).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: