{"record":{"id":"794cabd258506c24","repo":"t8y2/dbx","slug":"invalid-findone-option-sort-expected-an-object","errorCode":null,"errorMessage":"Invalid findOne option sort: expected an object","messagePattern":"Invalid findOne option sort: expected an object","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":741,"sourceCode":"    private static Object findOne(JsonObject params) {\n        MongoClient c = requireClient();\n        String database = params.get(\"database\").getAsString();\n        String collection = params.get(\"collection\").getAsString();\n        Document filter = documentOrNull(params, \"filter\");\n        Document projection = documentOrNull(params, \"projection\");\n        Document options = documentOrNull(params, \"options\");\n        Document sort = null;\n\n        if (options != null) {\n            for (String key : options.keySet()) {\n                if (!\"sort\".equals(key)) {\n                    throw new IllegalArgumentException(\"Unsupported findOne option: \" + key);\n                }\n            }\n            Object rawSort = options.get(\"sort\");\n            if (rawSort != null) {\n                if (!(rawSort instanceof Document sortDocument)) {\n                    throw new IllegalArgumentException(\"Invalid findOne option sort: expected an object\");\n                }\n                sort = sortDocument;\n            }\n        }\n\n        var iterable = c.getDatabase(database).getCollection(collection).find(filter == null ? new Document() : filter);\n        if (projection != null) {\n            iterable = iterable.projection(projection);\n        }\n        if (sort != null) {\n            iterable = iterable.sort(sort);\n        }\n        Document document = iterable.limit(1).first();\n\n        List<Map<String, Object>> documents = new ArrayList<>();\n        List<JsonObject> extendedDocuments = new ArrayList<>();\n        if (document != null) {\n            documents.add(bsonToJson(document));","sourceCodeStart":723,"sourceCodeEnd":759,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L723-L759","documentation":"When findOne is given options.sort, the value must be a MongoDB Document (an object) mapping field names to sort directions. If 'sort' is present but is not an object (e.g. a string or array), this error is thrown.","triggerScenarios":"Calling findOne with options.sort as a string like \"name\", a JSON array like [[\"name\",1]], or null-coerced primitives.","commonSituations":"Passing MongoDB shell-style sort strings; passing SQL-style 'ORDER BY name' strings; arrays of tuples from other driver conventions; JSON where sort was serialized as a string.","solutions":["Pass sort as an object: {\"field\":1} for ascending, {\"field\":-1} for descending","Convert array-of-tuples sort formats into an object before calling","Omit options.sort entirely if no sorting is needed","Parse/normalize sort input from clients into a Document"],"exampleFix":"// before\n{\"collection\":\"users\",\"options\":{\"sort\":\"name\"}}\n// after\n{\"collection\":\"users\",\"options\":{\"sort\":{\"name\":1}}}","handlingStrategy":"type-guard","validationCode":"if (opts && opts.sort != null && (typeof opts.sort !== 'object' || Array.isArray(opts.sort))) {\n  throw new Error('sort must be an object like {field: 1}');\n}","typeGuard":"function isSortDocument(v) {\n  return v == null || (typeof v === 'object' && !Array.isArray(v));\n}","tryCatchPattern":"try {\n  result = agent.findOne(params);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() === 'Invalid findOne option sort: expected an object') {\n    delete params.options.sort;\n    result = agent.findOne(params);\n  } else throw e;\n}","preventionTips":["Always express sort as {field: 1|-1} objects","Convert array-of-tuples sorts with Object.fromEntries","Never pass shell-style or SQL-style sort strings","Validate sort shape at API boundaries"],"tags":["mongodb","findone","sort","type-error"],"backgroundTag":"invalid-option-value","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}