{"record":{"id":"c480a04e876c259f","repo":"t8y2/dbx","slug":"only-regular-mongodb-collections-can-be-cloned-vi","errorCode":null,"errorMessage":"Only regular MongoDB collections can be cloned; views and time-series collections are not supported","messagePattern":"Only regular MongoDB collections can be cloned; views and time-series collections are not supported","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1169,"sourceCode":"     * 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));\n        long indexesCopied = cloneCollectionIndexes(database, source, sourceName, targetName);\n\n        Map<String, Object> result = new LinkedHashMap<>();\n        result.put(\"documents_copied\", documentsCopied);\n        result.put(\"indexes_copied\", indexesCopied);\n        return result;","sourceCodeStart":1151,"sourceCodeEnd":1187,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1151-L1187","documentation":"cloneCollection only supports plain (regular) MongoDB collections. If the source resolves to a view or a time-series collection, the agent throws because its clone mechanism (explicit createCollection + copy) cannot faithfully duplicate view pipelines or time-series options.","triggerScenarios":"Calling cloneCollection where source_collection names a view created with createView, or a time-series collection created with timeseries options; the spec from listCollections has a type other than 'collection' (e.g. 'view') or carries type:'timeseries' options.","commonSituations":"Trying to clone an aggregation view to materialize it; cloning IoT/metrics time-series collections; scripts that treat every name from listCollections as a regular collection.","solutions":["Recreate views with db.createView using the same pipeline on the target database","For time-series data, create a new time-series collection and copy documents, or use mongodump","Verify the collection type with listCollections before cloning"],"exampleFix":"// before\nagent.execute(\"cloneCollection\", params); // source is a view\n// after\nDocument spec = collectionSpecifications(db).stream()\n    .filter(d -> sourceName.equals(d.getString(\"name\")))\n    .findFirst().orElseThrow();\nif (\"collection\".equals(spec.getString(\"type\"))) {\n    agent.execute(\"cloneCollection\", params);\n} else {\n    db.createView(targetName, sourceName, pipeline); // handle views explicitly\n}","handlingStrategy":"validation","validationCode":"Document spec = db.listCollections()\n    .into(new ArrayList<>()).stream()\n    .filter(d -> sourceName.equals(d.getString(\"name\")))\n    .findFirst().orElseThrow();\nif (!\"collection\".equals(spec.getString(\"type\"))) {\n    throw new IllegalStateException(sourceName + \" is not a regular collection\");\n}","typeGuard":"boolean isRegularCollection(Document listCollectionsSpec) {\n    return \"collection\".equals(listCollectionsSpec.getString(\"type\"))\n        && !listCollectionsSpec.containsKey(\"options\");\n}","tryCatchPattern":"try {\n    agent.execute(\"cloneCollection\", params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"views and time-series\")) {\n        db.createView(targetName, sourceName, pipeline); // view-specific handling\n    } else { throw e; }\n}","preventionTips":["Check the 'type' field from listCollections before cloning","Recreate views with createView rather than cloning","Treat time-series collections with dedicated backup tooling"],"tags":["validation","mongodb","views","time-series"],"backgroundTag":"unsupported-collection-type","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"}