{"record":{"id":"b26db4b684d723ac","repo":"t8y2/dbx","slug":"system-collections-cannot-be-cloned","errorCode":null,"errorMessage":"System collections cannot be cloned","messagePattern":"System collections cannot be cloned","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1163,"sourceCode":"        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\n        MongoCollection<Document> source = database.getCollection(sourceName);\n        MongoCollection<Document> target = database.getCollection(targetName);\n        long documentsCopied = cloneCollectionDocuments(source, target, needsValidationBypass(collectionOptions));","sourceCodeStart":1145,"sourceCodeEnd":1181,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1145-L1181","documentation":"cloneCollection rejects any request whose source or target collection name starts with \"system.\", because MongoDB system collections are internal and must not be duplicated via user-driven clones. The check runs before any server-side work.","triggerScenarios":"Passing source_collection or target_collection starting with \"system.\" (e.g. \"system.js\", \"system.views\") to the cloneCollection action.","commonSituations":"Attempting to back up internal metadata collections like system.js; scripts iterating all collections returned by listCollections without filtering system.* entries; accidental typo prefixing a name with 'system.'.","solutions":["Remove system.* collections from the input (clone user collections only)","Filter out names starting with 'system.' when enumerating collections programmatically","Use mongodump/mongorestore or $out for legitimate internal-data backup needs"],"exampleFix":"// before\nparams.addProperty(\"source_collection\", \"system.js\");\nparams.addProperty(\"target_collection\", \"system_js_copy\");\n// after\nString src = \"system.js\";\nif (!src.startsWith(\"system.\")) {\n    params.addProperty(\"source_collection\", src);\n    params.addProperty(\"target_collection\", src + \"_copy\");\n}","handlingStrategy":"validation","validationCode":"if (sourceName.startsWith(\"system.\") || targetName.startsWith(\"system.\")) {\n    throw new IllegalArgumentException(\"system.* collections cannot be cloned\");\n}","typeGuard":"boolean isUserCollection(String name) {\n    return name != null && !name.startsWith(\"system.\");\n}","tryCatchPattern":"try {\n    agent.execute(\"cloneCollection\", params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"System collections\")) {\n        // skip or back up via mongodump instead\n    } else { throw e; }\n}","preventionTips":["Filter out 'system.*' names when iterating listCollections output","Use dump/restore tooling for internal collections, not cloneCollection","Add an allowlist of cloneable collection prefixes"],"tags":["validation","mongodb","system-collections"],"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"}