{"id":"297ec4b548ede037","repo":"mongodb/node-mongodb-native","slug":"a-query-document-must-be-specified-for-query","errorCode":null,"errorMessage":"A query document must be specified for query","messagePattern":"A query document must be specified for query","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/commands.ts","lineNumber":98,"sourceCode":"  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;\n    this.returnFieldSelector = options.returnFieldSelector || undefined;\n    this.requestId = options.requestId ?? OpQueryRequest.getRequestId();\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/commands.ts#L80-L116","documentation":"Thrown by the OpQueryRequest constructor (commands.ts:98) as a MongoRuntimeError when the query document is null or undefined. The OP_QUERY message must contain a query body to send to the server; an empty one is rejected at construction. Internal invariant - the public API always supplies a query document.","triggerScenarios":"Internal code calling new OpQueryRequest(db, null, options) or new OpQueryRequest(db, undefined, options). Reachable only through driver internals or a non-standard subclass.","commonSituations":"Driver defect where a command builder returned null; extension code that forgot to pass a query document.","solutions":["Report as a driver bug with the operation and version that triggered it","Use the public Collection/Db APIs rather than constructing OpQueryRequest manually","If subclassing, always pass a non-null query document"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /query document must be specified/i.test(e.message)) {\n    // internal bug - report to driver maintainers\n  }\n  throw e;\n}","preventionTips":["Use the public Collection/Db APIs rather than internal wire-protocol classes","Report this as a driver bug if reached via normal usage"],"tags":["wire-protocol","internal","op-query","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}