#how to
1 messages · Page 1 of 1 (latest)
To index users by their Convex-assigned ID, you generally do not need to create a custom index, because every Convex document already has a unique _id field, and you can always retrieve a user by their _id using db.get(id). However, if you want to create a custom index on a different id field (for example, if you have a custom string id), you can add an index like this:
users: defineTable({
// ... your fields ...
id: v.string(), // your custom id field
// other fields...
}).index("byId", ["id"])
If you want to add an index on the built-in _id field, that's not necessary—Convex always allows efficient lookups by _id without a custom index.
If you are using the default users table from authTables, and you want to add a custom index, you can inline the table definition and add your index as shown above. For example, if you have a custom id field:
users: defineTable({
id: v.string(),
name: v.optional(v.string()),
// ... other fields ...
}).index("byId", ["id"])
But for the built-in _id, just use db.get(id)—no index needed.
For reference, see the example of adding an index on a custom id field in the Lucia Auth Convex schema example:
users: defineTable({
id: v.string(),
email: v.string(),
}).index("byId", ["id"])