{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/utils.ts#L1034-L1070","documentation":"Thrown by the shuffle() helper when its `limit` argument exceeds the number of items in the sequence. shuffle() is an internal Fisher-Yates utility used to randomize server/SRV-record ordering for load distribution; the contract is that limit must be <= items.length. A violation is a programming error inside the driver, not user input, and surfaces as a MongoRuntimeError.","triggerScenarios":"Internal call site passing a limit larger than the candidate set (e.g. asking to shuffle more servers/SRV records than were discovered). Not reachable through a specific public API option.","commonSituations":"A regression in SDAM or SRV polling code computing an invalid limit; unusual cluster topologies exercising an untested shuffle path; an internal caller miscounting candidates after a topology change.","solutions":["Upgrade to the latest driver patch release; if the error reproduces on the latest version, file a driver bug with the topology details and stack trace.","If it correlates with a specific cluster shape (e.g. single host, all-non-empty SRV set), capture that context for the bug report.","As a workaround, avoid the triggering configuration (e.g. different SRV host) until patched."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (err) {\n  if (err instanceof MongoRuntimeError && /Limit must be less than the number of items/.test(err.message)) {\n    // internal driver defect; capture topology context and report\n    throw new Error('Driver internal error during server selection shuffle', { cause: err });\n  }\n  throw err;\n}","preventionTips":["Stay on the latest driver release where internal SDAM bugs are fixed.","If it reproduces with a specific cluster topology, capture that for a bug report.","Avoid exotic single-host SRV setups that may exercise untested shuffle paths."],"tags":["sdam","internal","srv"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}