{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L418-L454","documentation":"Thrown by FindCursor.allowDiskUse when no sort specification has been set. allowDiskUse is only meaningful for blocking sort operations that would otherwise exceed the 100MB memory limit; without a sort there is nothing for the disk spill to serve, so the driver rejects the call as a MongoInvalidArgumentError.","triggerScenarios":"collection.find({}).allowDiskUse(); calling .allowDiskUse() before .sort(); constructing options where sort is conditionally set and the condition evaluated false.","commonSituations":"Adding allowDiskUse defensively to large-result queries; copy-pasting from a sorted query; setting allowDiskUse in a builder that does not guarantee a sort.","solutions":["Call cursor.sort(...) before cursor.allowDiskUse().","Remove allowDiskUse if the query does not sort; it has no effect.","Pass allowDiskUse only inside the same code path that sets sort."],"exampleFix":"// before\ncollection.find({ bigFilter: true }).allowDiskUse();\n\n// after\ncollection.find({ bigFilter: true }).sort({ ts: 1 }).allowDiskUse();","handlingStrategy":"validation","validationCode":"function safeAllowDiskUse(cursor, sortSpec) {\n  if (sortSpec == null) throw new Error('allowDiskUse requires a prior sort specification');\n  cursor.sort(sortSpec);\n  return cursor.allowDiskUse();\n}","typeGuard":"function hasSortSpec(findOptions) { return findOptions?.sort != null; }","tryCatchPattern":null,"preventionTips":["Only call allowDiskUse in the same code path that sets sort.","Treat allowDiskUse as a property of the sort, not the query."],"tags":["find","sort","allow-disk-use","options-validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}