{"id":"a07a732138c0f335","repo":"mongodb/node-mongodb-native","slug":"a-collection-name-must-be-determined-before-killcu","errorCode":null,"errorMessage":"A collection name must be determined before killCursors","messagePattern":"A collection name must be determined before killCursors","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/operations/kill_cursors.ts","lineNumber":41,"sourceCode":"  cursorId: Long;\n\n  constructor(cursorId: Long, ns: MongoDBNamespace, server: Server, options: OperationOptions) {\n    super(options);\n    this.ns = ns;\n    this.cursorId = cursorId;\n    this.server = server;\n  }\n\n  override get commandName() {\n    return 'killCursors' as const;\n  }\n\n  override buildCommand(_connection: Connection, _session?: ClientSession): KillCursorsCommand {\n    const killCursors = this.ns.collection;\n    if (killCursors == null) {\n      // Cursors should have adopted the namespace returned by MongoDB\n      // which should always defined a collection name (even a pseudo one, ex. db.aggregate())\n      throw new MongoRuntimeError('A collection name must be determined before killCursors');\n    }\n\n    const killCursorsCommand: KillCursorsCommand = {\n      killCursors,\n      cursors: [this.cursorId]\n    };\n\n    return killCursorsCommand;\n  }\n\n  override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {\n    return {\n      session: this.session,\n      timeoutContext\n    };\n  }\n\n  override handleError(_error: MongoError): void {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/kill_cursors.ts#L23-L59","documentation":"Thrown by KillCursorsOperation.buildCommand when this.ns.collection is null/undefined, so the killCursors command has no collection to target. The driver sends killCursors to clean up server-side cursors when iteration stops early; if the cursor's namespace lacks a collection name, cleanup cannot proceed. It is a MongoRuntimeError and, per spec, killCursors errors are otherwise swallowed by handleError - this particular throw happens during command construction, before that suppression.","triggerScenarios":"A cursor with an incomplete namespace being closed/killed (e.g., breaking out of a for-await loop early on a database-level aggregation cursor, triggering automatic killCursors on cleanup). Same namespace-adoption gap as the getMore variant, but on the teardown path.","commonSituations":"Early-break from iterating db.aggregate()/change-stream cursors; driver version mismatch in namespace handling; internal/custom cursor construction.","solutions":["Prefer collection-scoped operations so cursors always carry a collection name.","Let cursors exhaust naturally where possible to avoid the killCursors path.","If standard operations reproduce this, report it as a driver bug with topology and version details.","Avoid manually closing or constructing cursors outside the public API."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await cursor.close();\n} catch (e) {\n  // killCursors errors are usually safe to ignore; a 'collection name' runtime error\n  // indicates a namespace gap - log and continue rather than aborting cleanup.\n  if (!(e instanceof MongoRuntimeError && /collection name/.test(e.message))) throw e;\n}","preventionTips":["Let cursors exhaust naturally where possible to avoid the killCursors path.","Use collection-scoped operations so the namespace is always complete.","Don't construct cursors outside the public API."],"tags":["cursor","kill-cursors","namespace","runtime-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}