{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/run_command_cursor.ts#L83-L119","documentation":"RunCommandCursor deliberately overrides clone() to throw because the cursor wraps an arbitrary user-supplied command that the driver cannot safely re-execute or fork. Each RunCommandCursor owns a frozen command and a single execution lifecycle, so obtaining an independent cursor requires invoking db.runCursorCommand() again.","triggerScenarios":"Calling .clone() on a cursor returned by db.runCursorCommand(cmd). This usually happens when porting generic cursor utilities that worked against FindCursor/AggregationCursor.","commonSituations":"Migrating find()-based helpers to runCursorCommand(); shared middleware that normalizes cursors via clone(); code that assumed every AbstractCursor supports clone().","solutions":["Replace cursor.clone() with a fresh db.runCursorCommand(cursor.command) call","Keep the original command document and re-invoke runCursorCommand for each independent iteration","Branch generic cursor helpers to skip clone() for RunCommandCursor instances"],"exampleFix":"// before\nconst c2 = cursor.clone();\n// after\nconst c2 = db.runCursorCommand(cursor.command);","handlingStrategy":"type-guard","validationCode":"if (cursor instanceof RunCommandCursor) {\n  throw new Error('RunCommandCursor cannot be cloned; call db.runCursorCommand again');\n}","typeGuard":"import { RunCommandCursor } from 'mongodb';\nfunction isCloneable(cursor: AbstractCursor): boolean {\n  return !(cursor instanceof RunCommandCursor);\n}","tryCatchPattern":null,"preventionTips":["Treat RunCommandCursor as non-cloneable by design","Keep the original command document so you can re-invoke runCursorCommand","Branch generic cursor utilities on instanceof RunCommandCursor"],"tags":["cursor","runcommandcursor","api-misuse"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}