{"record":{"id":"4a88f87d34a22831","repo":"t8y2/dbx","slug":"target-collection-name-must-differ-from-the-source","errorCode":null,"errorMessage":"Target collection name must differ from the source collection name","messagePattern":"Target collection name must differ from the source collection name","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1160,"sourceCode":"        MongoClient c = requireClient();\n        String database = params.get(\"database\").getAsString();\n        String collection = params.get(\"collection\").getAsString();\n        c.getDatabase(database).getCollection(collection).drop();\n        return Collections.singletonMap(\"ok\", true);\n    }\n\n    /**\n     * Clone a regular collection with the commands available to the legacy\n     * driver. This keeps older MongoDB servers on the same full-clone path as\n     * the native driver instead of silently copying documents only.\n     */\n    private static Object cloneCollection(JsonObject params) {\n        MongoClient client = requireClient();\n        String databaseName = params.get(\"database\").getAsString();\n        String sourceName = params.get(\"source_collection\").getAsString();\n        String targetName = params.get(\"target_collection\").getAsString();\n        if (sourceName.equals(targetName)) {\n            throw new IllegalArgumentException(\"Target collection name must differ from the source collection name\");\n        }\n        if (sourceName.startsWith(\"system.\") || targetName.startsWith(\"system.\")) {\n            throw new IllegalArgumentException(\"System collections cannot be cloned\");\n        }\n\n        MongoDatabase database = client.getDatabase(databaseName);\n        Document sourceSpecification = requireCollectionSpecification(database, sourceName);\n        if (!isRegularCollectionSpecification(sourceSpecification)) {\n            throw new IllegalArgumentException(\n                \"Only regular MongoDB collections can be cloned; views and time-series collections are not supported\"\n            );\n        }\n\n        Document collectionOptions = collectionOptions(sourceSpecification);\n        // Explicit creation ensures the source is never merged into an\n        // existing target collection.\n        database.runCommand(cloneCreateCollectionCommand(targetName, sourceSpecification));\n","sourceCodeStart":1142,"sourceCodeEnd":1178,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1142-L1178","documentation":"MongoAgent.cloneCollection refuses to clone a collection onto itself. Cloning requires a distinct target name, so when source_collection equals target_collection this IllegalArgumentException is thrown before any database operation runs.","triggerScenarios":"Calling the cloneCollection tool/action with params where source_collection and target_collection are identical strings (e.g. both \"orders\").","commonSituations":"Copy-paste of parameters where the target field was never edited; programmatically building params with the same variable used for both names; UI defaults prefilling the target with the source name.","solutions":["Set target_collection to a name that differs from source_collection","Validate the two names in caller code before invoking cloneCollection","If you only wanted a snapshot, pick a suffixed name like source + '_backup'"],"exampleFix":"// before\nparams.addProperty(\"source_collection\", \"orders\");\nparams.addProperty(\"target_collection\", \"orders\");\n// after\nparams.addProperty(\"source_collection\", \"orders\");\nparams.addProperty(\"target_collection\", \"orders_backup_2026_09\");","handlingStrategy":"validation","validationCode":"if (sourceName.equals(targetName)) {\n    throw new IllegalArgumentException(\"target_collection must differ from source_collection\");\n}\nparams.addProperty(\"source_collection\", sourceName);\nparams.addProperty(\"target_collection\", targetName);","typeGuard":"boolean isValidCloneRequest(String src, String tgt) {\n    return src != null && tgt != null && !src.equals(tgt);\n}","tryCatchPattern":"try {\n    agent.execute(\"cloneCollection\", params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"must differ\")) {\n        params.addProperty(\"target_collection\", sourceName + \"_copy\");\n        agent.execute(\"cloneCollection\", params);\n    } else { throw e; }\n}","preventionTips":["Always derive the target name programmatically (e.g. source + suffix) instead of typing it separately","Validate source != target in caller code before invoking","Never reuse one variable for both parameter fields"],"tags":["validation","mongodb","arguments"],"backgroundTag":"invalid-argument-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"}