{"record":{"id":"cb791432ed8c0961","repo":"t8y2/dbx","slug":"index-keys-are-required","errorCode":null,"errorMessage":"Index keys are required","messagePattern":"Index keys are required","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1003,"sourceCode":"    }\n\n    private static boolean serverRequiresSerialDropIndexes(MongoClient client, String database) {\n        try {\n            Document buildInfo = client.getDatabase(database).runCommand(new Document(\"buildInfo\", 1));\n            return serverVersionRequiresSerialDropIndexes(serverVersionFromBuildInfo(buildInfo));\n        } catch (RuntimeException error) {\n            // Without an explicit old version, preserve MongoDB's single-command array semantics.\n            return false;\n        }\n    }\n\n    private static Object createIndex(JsonObject params) {\n        MongoClient c = requireClient();\n        String database = params.get(\"database\").getAsString();\n        String collection = params.get(\"collection\").getAsString();\n        Document keys = requiredDocument(params, \"keys_json\", \"Index keys\");\n        if (keys.isEmpty()) {\n            throw new IllegalArgumentException(\"Index keys are required\");\n        }\n\n        Document index = new Document(\"key\", keys);\n        Document options = documentOrNull(params, \"options_json\");\n        if (options != null) {\n            if (options.containsKey(\"key\")) {\n                throw new IllegalArgumentException(\"Index options cannot contain \\\"key\\\"; specify index fields in keys JSON\");\n            }\n            index.putAll(options);\n        }\n        String name;\n        if (!index.containsKey(\"name\")) {\n            name = defaultIndexName(keys);\n            index.put(\"name\", name);\n        } else if (!(index.get(\"name\") instanceof String)) {\n            throw new IllegalArgumentException(\"Index option \\\"name\\\" must be a non-empty string\");\n        } else {\n            name = (String) index.get(\"name\");","sourceCodeStart":985,"sourceCodeEnd":1021,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L985-L1021","documentation":"createIndex builds a MongoDB createIndexes command whose index specification needs a \"key\" document mapping fields to sort directions. After the keys_json parameter passes the requiredDocument check (present), the agent additionally rejects an empty keys document. An index with no keys is meaningless, so it throws IllegalArgumentException.","triggerScenarios":"Calling createIndex with {\"keys_json\": {}} — the parameter is present but contains no fields.","commonSituations":"Templates or generated calls leaving keys_json empty; a serialization bug producing an empty map; copying an example and deleting the keys to fill in later.","solutions":["Supply the index key fields, e.g. {\"keys_json\": {\"field\": 1}} or {\"createdAt\": -1}.","For compound indexes include every field, e.g. {\"userId\": 1, \"createdAt\": -1}.","If keys are computed at runtime, assert the resulting map is non-empty before calling createIndex."],"exampleFix":"// before\n{\"database\": \"app\", \"collection\": \"orders\", \"keys_json\": {}}\n// after\n{\"database\": \"app\", \"collection\": \"orders\", \"keys_json\": {\"userId\": 1, \"createdAt\": -1}}","handlingStrategy":"validation","validationCode":"if (!params.keys_json || Object.keys(params.keys_json).length === 0) {\n  throw new Error(\"keys_json must contain at least one field, e.g. {field: 1}\");\n}","typeGuard":"function hasIndexKeys(keys) {\n  return keys != null && typeof keys === \"object\" && Object.keys(keys).length > 0;\n}","tryCatchPattern":"try {\n  await agent.createIndex({ database, collection, keys_json: keys, options_json: opts });\n} catch (e) {\n  if (String(e.message).includes(\"Index keys are required\")) {\n    console.error(\"Supply keys_json like {field: 1}\");\n  } else throw e;\n}","preventionTips":["Always pass at least one field:direction pair in keys_json.","Never leave keys_json as {} in templates.","For compound indexes list every field explicitly."],"tags":["mongodb","index","validation","empty-input"],"backgroundTag":"missing-required-parameter","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}