{"record":{"id":"ef37f36d28756c81","repo":"mongodb/node-mongodb-native","slug":"unexpected-null-cursor-id-a-cursor-creating-comma","errorCode":null,"errorMessage":"Unexpected null cursor id. A cursor creating command should have set this","messagePattern":"Unexpected null cursor id\\. A cursor creating command should have set this","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"critical","filePath":"src/cursor/abstract_cursor.ts","lineNumber":861,"sourceCode":"\n      this.cursorSession = null;\n    }\n  }\n\n  /**\n   * Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance\n   */\n  abstract clone(): AbstractCursor<TSchema>;\n\n  /** @internal */\n  protected abstract _initialize(\n    session: ClientSession | undefined\n  ): Promise<InitialCursorResponse>;\n\n  /** @internal */\n  async getMore(): Promise<CursorResponse> {\n    if (this.cursorId == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null cursor id. A cursor creating command should have set this'\n      );\n    }\n    if (this.selectedServer == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null selectedServer. A cursor creating command should have set this'\n      );\n    }\n\n    if (this.cursorSession == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null session. A cursor creating command should have set this'\n      );\n    }\n    const getMoreOptions = {\n      ...this.cursorOptions,\n      session: this.cursorSession,\n      batchSize: this.cursorOptions.batchSize","sourceCodeStart":843,"sourceCodeEnd":879,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/abstract_cursor.ts#L843-L879","documentation":"Thrown as a MongoRuntimeError from getMore() when cursorId is null at the time a getMore is requested. cursorId is set from the server's response during cursorInit(); reaching getMore() with it still null means the cursor was never successfully initialized (the initial command did not run or did not store a response). This is a driver-internal invariant violation, not a user input error: it indicates either a bug in the driver or that cursor initialization was bypassed/short-circuited. getMore() is marked @internal, so direct user invocation is unsupported.","triggerScenarios":"Driver internals call getMore() before cursorInit() completed; a subclass overrides _initialize() to return without a proper response; reflection or test code calls the internal getMore() directly on a never-run cursor.","commonSituations":"Hitting a genuine driver bug where initialization raced with iteration; instrumenting or monkey-patching cursor internals; an unhandled rejection during _initialize() leaving the cursor half-initialized.","solutions":["Report the issue to the driver maintainers with a reproduction, since this is an invariant the driver itself guarantees.","Ensure you only iterate via the public API (next(), hasNext(), for-await, toArray()) so initialization is always driven by fetchBatch().","If subclassing, implement _initialize() to return a valid InitialCursorResponse with a non-null response.id.","Upgrade to the latest driver patch release in case the bug is already fixed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await cursor.next();\n} catch (err) {\n  if (err instanceof MongoRuntimeError && /null cursor id/.test(err.message)) {\n    // driver invariant violated: report and recreate the cursor\n    cursor = collection.find(filter, opts);\n  } else {\n    throw err;\n  }\n}","preventionTips":["Do not call the @internal getMore() directly; use the public iteration API.","Implement _initialize() correctly if subclassing (return a response with an id).","Keep the driver up to date; report suspected invariant violations with a reproduction."],"tags":["cursor","internal-invariant","runtime-error","bug"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}