{"id":"6a7ea23b7b5de5e6","repo":"mongodb/node-mongodb-native","slug":"option-allowdiskuse-requires-a-sort-specificatio","errorCode":null,"errorMessage":"Option \"allowDiskUse\" requires a sort specification","messagePattern":"Option \"allowDiskUse\" requires a sort specification","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":436,"sourceCode":"    if (this.findOptions.tailable) {\n      throw new MongoTailableCursorError('Tailable cursor does not support sorting');\n    }\n\n    this.findOptions.sort = formatSort(sort, direction);\n    return this;\n  }\n\n  /**\n   * Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher)\n   *\n   * @remarks\n   * {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}\n   */\n  allowDiskUse(allow = true): this {\n    this.throwIfInitialized();\n\n    if (!this.findOptions.sort) {\n      throw new MongoInvalidArgumentError('Option \"allowDiskUse\" requires a sort specification');\n    }\n\n    // As of 6.0 the default is true. This allows users to get back to the old behavior.\n    if (!allow) {\n      this.findOptions.allowDiskUse = false;\n      return this;\n    }\n\n    this.findOptions.allowDiskUse = true;\n    return this;\n  }\n\n  /**\n   * Set the collation options for the cursor.\n   *\n   * @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).\n   */\n  collation(value: CollationOptions): this {","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L418-L454","documentation":"Thrown by FindCursor.allowDiskUse() (MongoInvalidArgumentError) when no sort specification has been set on the cursor. allowDiskUse only matters for blocking sort operations that may exceed the 100MB memory limit; without a sort it has no effect, so the driver requires .sort() to be called first.","triggerScenarios":"coll.find({}).allowDiskUse() without a preceding .sort(...).","commonSituations":"Copy-pasting allowDiskUse into find chains that don't sort; enabling it defensively as a 'performance flag'.","solutions":["Call .sort(...) before .allowDiskUse()","Remove the allowDiskUse() call if you are not sorting","On MongoDB >= 6.0 allowDiskUse defaults to true for finds that need it; the explicit call is usually unnecessary"],"exampleFix":"// before\ncoll.find({ bigQuery }).allowDiskUse();\n// after\ncoll.find({ bigQuery }).sort({ field: 1 }).allowDiskUse();","handlingStrategy":"validation","validationCode":"function allowDiskUseSafe(cursor, allow = true) {\n  if (!cursor.findOptions?.sort) {\n    throw new Error('allowDiskUse requires a prior .sort() call');\n  }\n  return cursor.allowDiskUse(allow);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call .sort() before .allowDiskUse()","Remove allowDiskUse() if not sorting","On MongoDB >= 6.0 the server defaults allowDiskUse to true where relevant"],"tags":["find","sort","allowdiskuse","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}