t8y2/dbx · error · java.lang.IllegalArgumentException

Only regular MongoDB collections can be cloned; views and ti

Error message

Only regular MongoDB collections can be cloned; views and time-series collections are not supported

What it means

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.

Source

Thrown at agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java:1169

     * driver. This keeps older MongoDB servers on the same full-clone path as
     * the native driver instead of silently copying documents only.
     */
    private static Object cloneCollection(JsonObject params) {
        MongoClient client = requireClient();
        String databaseName = params.get("database").getAsString();
        String sourceName = params.get("source_collection").getAsString();
        String targetName = params.get("target_collection").getAsString();
        if (sourceName.equals(targetName)) {
            throw new IllegalArgumentException("Target collection name must differ from the source collection name");
        }
        if (sourceName.startsWith("system.") || targetName.startsWith("system.")) {
            throw new IllegalArgumentException("System collections cannot be cloned");
        }

        MongoDatabase database = client.getDatabase(databaseName);
        Document sourceSpecification = requireCollectionSpecification(database, sourceName);
        if (!isRegularCollectionSpecification(sourceSpecification)) {
            throw new IllegalArgumentException(
                "Only regular MongoDB collections can be cloned; views and time-series collections are not supported"
            );
        }

        Document collectionOptions = collectionOptions(sourceSpecification);
        // Explicit creation ensures the source is never merged into an
        // existing target collection.
        database.runCommand(cloneCreateCollectionCommand(targetName, sourceSpecification));

        MongoCollection<Document> source = database.getCollection(sourceName);
        MongoCollection<Document> target = database.getCollection(targetName);
        long documentsCopied = cloneCollectionDocuments(source, target, needsValidationBypass(collectionOptions));
        long indexesCopied = cloneCollectionIndexes(database, source, sourceName, targetName);

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("documents_copied", documentsCopied);
        result.put("indexes_copied", indexesCopied);
        return result;

View on GitHub (pinned to c0390bff16)

Solutions

  1. Recreate views with db.createView using the same pipeline on the target database
  2. For time-series data, create a new time-series collection and copy documents, or use mongodump
  3. Verify the collection type with listCollections before cloning

Example fix

// before
agent.execute("cloneCollection", params); // source is a view
// after
Document spec = collectionSpecifications(db).stream()
    .filter(d -> sourceName.equals(d.getString("name")))
    .findFirst().orElseThrow();
if ("collection".equals(spec.getString("type"))) {
    agent.execute("cloneCollection", params);
} else {
    db.createView(targetName, sourceName, pipeline); // handle views explicitly
}
Defensive patterns

Strategy: validation

Validate before calling

Document spec = db.listCollections()
    .into(new ArrayList<>()).stream()
    .filter(d -> sourceName.equals(d.getString("name")))
    .findFirst().orElseThrow();
if (!"collection".equals(spec.getString("type"))) {
    throw new IllegalStateException(sourceName + " is not a regular collection");
}

Type guard

boolean isRegularCollection(Document listCollectionsSpec) {
    return "collection".equals(listCollectionsSpec.getString("type"))
        && !listCollectionsSpec.containsKey("options");
}

Try / catch

try {
    agent.execute("cloneCollection", params);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("views and time-series")) {
        db.createView(targetName, sourceName, pipeline); // view-specific handling
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/c480a04e876c259f. Report an issue: GitHub.