{"id":"4270785c7e1bb5e8","repo":"mongodb/node-mongodb-native","slug":"database-name-must-be-a-string-for-a-query","errorCode":null,"errorMessage":"Database name must be a string for a query","messagePattern":"Database name must be a string for a query","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/commands.ts","lineNumber":95,"sourceCode":"  batchSize: number;\n  tailable: boolean;\n  secondaryOk: boolean;\n  oplogReplay: boolean;\n  noCursorTimeout: boolean;\n  awaitData: boolean;\n  exhaust: boolean;\n  partial: boolean;\n  /** moreToCome is an OP_MSG only concept */\n  moreToCome = false;\n  databaseName: string;\n  query: Document;\n\n  constructor(databaseName: string, query: Document, options: OpQueryOptions) {\n    // Basic options needed to be passed in\n    // TODO(NODE-3483): Replace with MongoCommandError\n    const ns = `${databaseName}.$cmd`;\n    if (typeof databaseName !== 'string') {\n      throw new MongoRuntimeError('Database name must be a string for a query');\n    }\n    // TODO(NODE-3483): Replace with MongoCommandError\n    if (query == null) throw new MongoRuntimeError('A query document must be specified for query');\n\n    // Validate that we are not passing 0x00 in the collection name\n    if (ns.indexOf('\\x00') !== -1) {\n      // TODO(NODE-3483): Use MongoNamespace static method\n      throw new MongoRuntimeError('Namespace cannot contain a null character');\n    }\n\n    // Basic optionsa\n    this.databaseName = databaseName;\n    this.query = query;\n    this.ns = ns;\n\n    // Additional options\n    this.numberToSkip = options.numberToSkip || 0;\n    this.numberToReturn = options.numberToReturn || 0;","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/commands.ts#L77-L113","documentation":"Thrown by the OpQueryRequest constructor (commands.ts:95) as a MongoRuntimeError when databaseName is not a string. OP_QUERY is the legacy wire protocol message; the database name is required to build the namespace ('db.$cmd'). A non-string value indicates an internal caller passed invalid arguments - this is an internal invariant, not user-reachable through the public API.","triggerScenarios":"Internal code constructing new OpQueryRequest(undefined, query, options) or passing a number/object as databaseName. Reachable only via driver internals or subclasses that bypass type checks.","commonSituations":"Driver bug where a namespace resolution returned undefined; custom wire-protocol extension code misusing the OpQueryRequest constructor.","solutions":["Report as a driver bug - include the operation that triggered it and the driver version","Avoid constructing OpQueryRequest directly - use the high-level collection/db APIs","If subclassing, ensure databaseName is always a string before calling super()"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /Database name must be a string/i.test(e.message)) {\n    // internal bug - report with operation + driver version\n  }\n  throw e;\n}","preventionTips":["Avoid constructing OpQueryRequest directly","Report internal invariant violations with a repro to driver maintainers","Pin the driver version while a bug is investigated"],"tags":["wire-protocol","internal","namespace","op-query"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}