Hey guys, I have this index

Hey guys, I have this index
.index("userId", ["userId", "dueDate", "status", "isActive"])
.index("userId", ["userId", "dueDate", "status", "isActive"])
bcoz, sometime I want to fetch data by "userId" with "dueDate" comparison and sometimes wanna fetch it by "userId" and "status", But Now when I'm creating the query, it's forcing me to use "dueDate" always just after the "userId" check and then only i can check "status". so, It means I'll have to create multiple indexes for this to work???? like this:
.index("userId", ["userId", "dueDate"]).index("by_user_status", ["userId", "status"]
.index("userId", ["userId", "dueDate"]).index("by_user_status", ["userId", "status"]
is there any other way to do this?? I don't want to create too many indexes, as it will affect the performance obviously.
3 Replies
erquhart
erquhart7d ago
You must step through fields in index order, recommend reading the indexes doc here: https://docs.convex.dev/database/reading-data/indexes/#querying-documents-using-indexes So you would need two indexes to handle both userId+dueDate and userId+status
Clever Tagline
If it makes sense for your use case (e.g. the number of documents for a given userId won't be massive), you could keep the indexing simple by only creating an index for userId, then filter for the other options using normal JS filters.
Chandrabhan Singh
Thanks guys, 🙏🏻 both suggestions are helpful, I think, I'll be able to make it work now.

Did you find this page helpful?