{"record":{"id":"3ea6d0a57f7f5a53","repo":"mongodb/node-mongodb-native","slug":"clone-not-supported-create-a-new-cursor-with-db-r","errorCode":null,"errorMessage":"Clone not supported, create a new cursor with db.runCursorCommand","messagePattern":"Clone not supported, create a new cursor with db\\.runCursorCommand","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/cursor/run_command_cursor.ts","lineNumber":101,"sourceCode":"   * @param maxTimeMS - the number of milliseconds to wait for new data\n   */\n  public setMaxTimeMS(maxTimeMS: number): this {\n    this.getMoreOptions.maxAwaitTimeMS = maxTimeMS;\n    return this;\n  }\n\n  /**\n   * Controls the `getMore.batchSize` field\n   * @param batchSize - the number documents to return in the `nextBatch`\n   */\n  public setBatchSize(batchSize: number): this {\n    this.getMoreOptions.batchSize = batchSize;\n    return this;\n  }\n\n  /** Unsupported for RunCommandCursor */\n  public override clone(): never {\n    throw new MongoAPIError('Clone not supported, create a new cursor with db.runCursorCommand');\n  }\n\n  /** Unsupported for RunCommandCursor: readConcern must be configured directly on command document */\n  public override withReadConcern(_: ReadConcernLike): never {\n    throw new MongoAPIError(\n      'RunCommandCursor does not support readConcern it must be attached to the command being run'\n    );\n  }\n\n  /** Unsupported for RunCommandCursor: various cursor flags must be configured directly on command document */\n  public override addCursorFlag(_: string, __: boolean): never {\n    throw new MongoAPIError(\n      'RunCommandCursor does not support cursor flags, they must be attached to the command being run'\n    );\n  }\n\n  /**\n   * Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/run_command_cursor.ts#L83-L119","documentation":"Thrown by RunCommandCursor.clone() because cloning is intentionally unsupported for raw command cursors. Unlike find/aggregate cursors whose state is reconstructable from declarative options, RunCommandCursor freezes an arbitrary user command document and exposes mutable getMore options, so a deep clone would silently diverge from the original command. Re-create the cursor from db.runCursorCommand instead.","triggerScenarios":"Calling cursor.clone() on a RunCommandCursor returned by db.runCursorCommand(cmd, opts); generic code that clones every cursor before iteration; sharing a cursor across two consumers via clone().","commonSituations":"Generic cursor-pooling utilities that clone defensively; refactoring from find().clone() patterns to runCursorCommand without updating the clone call.","solutions":["Re-create the cursor: const c2 = db.runCursorCommand(cmd, opts) instead of cursor.clone().","Iterate the same cursor once and buffer results if you need two passes.","Remove the clone() call from generic cursor handling when the cursor is a RunCommandCursor."],"exampleFix":"// before\nconst c2 = cursor.clone();\n\n// after\nconst c2 = db.runCursorCommand(cursor.command, originalOptions);","handlingStrategy":"type-guard","validationCode":"function cloneOrRecreate(db, cursor, cmd, opts) {\n  if (cursor instanceof RunCommandCursor) return db.runCursorCommand(cmd, opts);\n  return cursor.clone();\n}","typeGuard":"function isRunCommandCursor(cursor) { return cursor instanceof RunCommandCursor; }","tryCatchPattern":null,"preventionTips":["Branch generic cursor utilities to recreate rather than clone RunCommandCursor.","Store the original command+options when you may need a second cursor."],"tags":["run-command-cursor","clone","unsupported"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}