From 21c4a2b0309c252fe7d4874ab19843afebb0fc5b Mon Sep 17 00:00:00 2001 From: Brandon Pereira Date: Mon, 1 Jun 2026 16:16:29 -0600 Subject: [PATCH] perf(api): add unique index on accessKey in User model (HDX-4403) Add a MongoDB index on the accessKey field in the User schema to eliminate full collection scans during API key authentication. The User.findOne({ accessKey }) query is called on every External API v2 and MCP request via the validateUserAccessKey middleware. Without an index, this results in a full collection scan on the users collection. Follows the same pattern as the existing email unique index. --- .changeset/add-accesskey-index.md | 5 +++++ packages/api/src/models/user.ts | 1 + 2 files changed, 6 insertions(+) create mode 100644 .changeset/add-accesskey-index.md diff --git a/.changeset/add-accesskey-index.md b/.changeset/add-accesskey-index.md new file mode 100644 index 0000000000..4a9b44d73c --- /dev/null +++ b/.changeset/add-accesskey-index.md @@ -0,0 +1,5 @@ +--- +"@hyperdx/api": minor +--- + +Add unique MongoDB index on accessKey field in User model to eliminate full collection scans during API key authentication. This could cause startup failures if any existing users share duplicate accessKey values. diff --git a/packages/api/src/models/user.ts b/packages/api/src/models/user.ts index b7067b20c3..8f60fadeb5 100644 --- a/packages/api/src/models/user.ts +++ b/packages/api/src/models/user.ts @@ -47,5 +47,6 @@ UserSchema.plugin(passportLocalMongoose, { }); UserSchema.index({ email: 1 }, { unique: true }); +UserSchema.index({ accessKey: 1 }, { unique: true }); export default mongoose.model('User', UserSchema);