#.

3 messages · Page 1 of 1 (latest)

mystic matrix
#

can you index values in array in convex db documents

brittle sandalBOT
#

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

#

No, you currently cannot index array values directly in Convex (for example, you can’t have an index that lets you efficiently query “documents where tags array contains X”). [Many-to-many arrays; Array indexing thread]

Because of this:

  • Using an array field is fine when:

    • You only need to read the array from the owning document, and
    • You don’t need to efficiently query “who has this element in their array,” and
    • The arrays stay relatively small. [Many-to-many arrays]
  • If you do need efficient lookups by an element (e.g. “all users with choice X”, “all products with tag Y”), the recommended pattern is a separate join/relationship table (e.g. { userId, choiceId }) with indexes on those scalar fields. [Many-to-many arrays; Array indexing thread]

There is also a note from Convex staff that array indexing is not on the near-term roadmap and that join tables are the expected solution for now. [Array indexing thread]

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