RJR
Convex Community2y ago
4 replies
RJ

Filter by documents with fields which are equal to a set (>= 2) of values using an index?

Say I have a table that looks like this:

books: defineTable({
  author: v.string(),
  content: v.string(),
}).index("by_author", ["author"])


and I'd like to retrieve a collection of documents belonging to the authors "Stendhal" and "Alexandre Dumas". If I use .filter, I understand that this is achievable in one query:

db.query("books")
  .filter((q) =>
    q.or(
      q.eq(q.field("author"), "Stendhal"),
      q.eq(q.field("author"), "Alexandre Dumas"),
    )
  )
  .collect()


but is it possible to achieve in one query using an index?

db.query("books")
  .withIndex(
    "by_author",
    (q) => q.eq("author", "Stendhal")
    // * No `q.or` available
    // * Further chaining with
    //   q.eq(...).eq(...) doesn't work,
    //   according to the types
    // * Neither may additional indexes be used
  )
Was this page helpful?