{"record":{"id":"104b2a611f3bb642","repo":"mongodb/node-mongodb-native","slug":"limit-must-be-less-than-the-number-of-items","errorCode":null,"errorMessage":"Limit must be less than the number of items","messagePattern":"Limit must be less than the number of items","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/utils.ts","lineNumber":1052,"sourceCode":"      return true;\n    }\n  }\n\n  return false;\n}\n\n/**\n * Fisher–Yates Shuffle\n *\n * Reference: https://bost.ocks.org/mike/shuffle/\n * @param sequence - items to be shuffled\n * @param limit - Defaults to `0`. If nonzero shuffle will slice the randomized array e.g, `.slice(0, limit)` otherwise will return the entire randomized array.\n */\nexport function shuffle<T>(sequence: Iterable<T>, limit = 0): Array<T> {\n  const items = Array.from(sequence); // shallow copy in order to never shuffle the input\n\n  if (limit > items.length) {\n    throw new MongoRuntimeError('Limit must be less than the number of items');\n  }\n\n  let remainingItemsToShuffle = items.length;\n  const lowerBound = limit % items.length === 0 ? 1 : items.length - limit;\n  while (remainingItemsToShuffle > lowerBound) {\n    // Pick a remaining element\n    const randomIndex = Math.floor(Math.random() * remainingItemsToShuffle);\n    remainingItemsToShuffle -= 1;\n\n    // And swap it with the current element\n    const swapHold = items[remainingItemsToShuffle];\n    items[remainingItemsToShuffle] = items[randomIndex];\n    items[randomIndex] = swapHold;\n  }\n\n  return limit % items.length === 0 ? items : items.slice(lowerBound);\n}\n","sourceCodeStart":1034,"sourceCodeEnd":1070,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/utils.ts#L1034-L1070","documentation":"Thrown by the internal shuffle() utility when the requested limit exceeds the number of items in the sequence. shuffle() performs a partial Fisher-Yates shuffle that requires limit <= items.length. Raised as MongoRuntimeError. This helper is used internally by SDAM (e.g. shuffling the server list for load distribution) and is not part of the public API.","triggerScenarios":"Internal SDAM code calling shuffle() with a limit larger than the available server/seed list. Not triggered by user-facing options directly; would indicate a driver bug in server-selection or topology description logic.","commonSituations":"Nearly never seen by users. Theoretically reachable if the topology's server count shrinks between computing a limit and calling shuffle(), or via a driver internal bug.","solutions":["Upgrade the driver to the latest stable version.","Report the full stack trace to the NODE driver Jira project with topology details.","If reproducible, reduce the cluster topology change rate (avoid rapid add/remove of nodes)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /Limit must be less than/.test(e.message)) {\n    // internal SDAM error: upgrade driver and report\n  } else throw e;\n}","preventionTips":["Keep the driver on the latest stable release.","Avoid rapid cluster topology changes (adding/removing many nodes in quick succession).","Report persistent occurrences with topology details to the driver project."],"tags":["internal","sdam","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}