{"record":{"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":"warning","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/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/commands.ts#L77-L113","documentation":"Thrown as a MongoRuntimeError in the OpQueryRequest constructor when databaseName is not typeof string. OpQueryRequest is the legacy OP_QUERY wire protocol message (pre-MongoDB 3.2), used internally for some monitoring/handshake paths. This is a defensive internal invariant check — the public API always supplies a string.","triggerScenarios":"Internal driver code constructs an OpQueryRequest with a non-string databaseName. Effectively unreachable from user code; would indicate a bug in driver internals or a faulty mock in tests.","commonSituations":"Driver bug, an incorrectly written test mock that passes a non-string, or a fork that calls the internal constructor directly with bad data.","solutions":["Report a driver bug with the stack trace — databaseName should always be a string internally","If testing with mocks, ensure the databaseName argument is a string","Avoid constructing OpQueryRequest directly; it is marked @internal"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isStringDbName(name: unknown): name is string {\n  return typeof name === 'string';\n}","tryCatchPattern":null,"preventionTips":["Do not call the internal OpQueryRequest constructor directly","Report occurrences as driver bugs with a stack trace"],"tags":["wire-protocol","internal","op-query"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}