{"id":"651c7baa14823e18","repo":"mongodb/node-mongodb-native","slug":"error-with-profile-command","errorCode":null,"errorMessage":"Error with profile command","messagePattern":"Error with profile command","errorType":"exception","errorClass":"MongoUnexpectedServerResponseError","httpStatus":null,"severity":"error","filePath":"src/operations/profiling_level.ts","lineNumber":43,"sourceCode":"  }\n\n  override get commandName() {\n    return 'profile' as const;\n  }\n\n  override buildCommandDocument(_connection: Connection): Document {\n    return { profile: -1 };\n  }\n\n  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string {\n    if (response.ok === 1) {\n      const was = response.was;\n      if (was === 0) return 'off';\n      if (was === 1) return 'slow_only';\n      if (was === 2) return 'all';\n      throw new MongoUnexpectedServerResponseError(`Illegal profiling level value ${was}`);\n    } else {\n      throw new MongoUnexpectedServerResponseError('Error with profile command');\n    }\n  }\n}\n","sourceCodeStart":25,"sourceCodeEnd":47,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/profiling_level.ts#L25-L47","documentation":"Thrown by ProfilingLevelOperation.handleOk when the server's profile command returns ok !== 1, i.e., the command itself failed but the response was still parsed as a success envelope. A MongoUnexpectedServerResponseError - the driver expected a successful profile result and got a failure-status document. Common upstream causes are insufficient privileges or running the command against a database/context where profiling is not allowed.","triggerScenarios":"Calling db.getProfilingLevel() as a non-privileged user (profiling requires the clusterMonitor or equivalent role / admin access); running it against a serverless or Atlas tier where profiling is restricted; the profile command being unavailable on the target deployment type.","commonSituations":"Connecting with an application read/write user instead of an admin/monitoring user; serverless MongoDB restrictions; managed-service permission scopes that block the profile command.","solutions":["Run db.runCommand({ profile: -1 }) directly to surface the server's actual error code/message (often a permissions failure).","Connect with a user that has the clusterMonitor or root/admin role on the admin database.","Confirm profiling is supported on your deployment (some managed tiers disable it).","Catch MongoUnexpectedServerResponseError and degrade gracefully in monitoring tooling that doesn't strictly need the level."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const level = await db.getProfilingLevel();\n} catch (e) {\n  if (e instanceof MongoUnexpectedServerResponseError && /profile command/.test(e.message)) {\n    // run db.runCommand({ profile: -1 }) to see the real server error (often permissions)\n  }\n  throw e;\n}","preventionTips":["Connect with a clusterMonitor/admin-capable user for profiling APIs.","Confirm profiling is supported on your deployment tier.","Guard admin-only calls in monitoring code paths."],"tags":["profiling","admin","permissions","server-response"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}