t8y2/dbx · error · java.lang.IllegalArgumentException

Target collection name must differ from the source collectio

Error message

Target collection name must differ from the source collection name

What it means

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.

Source

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

        MongoClient c = requireClient();
        String database = params.get("database").getAsString();
        String collection = params.get("collection").getAsString();
        c.getDatabase(database).getCollection(collection).drop();
        return Collections.singletonMap("ok", true);
    }

    /**
     * Clone a regular collection with the commands available to the legacy
     * 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));

View on GitHub (pinned to c0390bff16)

Solutions

  1. Set target_collection to a name that differs from source_collection
  2. Validate the two names in caller code before invoking cloneCollection
  3. If you only wanted a snapshot, pick a suffixed name like source + '_backup'

Example fix

// before
params.addProperty("source_collection", "orders");
params.addProperty("target_collection", "orders");
// after
params.addProperty("source_collection", "orders");
params.addProperty("target_collection", "orders_backup_2026_09");
Defensive patterns

Strategy: validation

Validate before calling

if (sourceName.equals(targetName)) {
    throw new IllegalArgumentException("target_collection must differ from source_collection");
}
params.addProperty("source_collection", sourceName);
params.addProperty("target_collection", targetName);

Type guard

boolean isValidCloneRequest(String src, String tgt) {
    return src != null && tgt != null && !src.equals(tgt);
}

Try / catch

try {
    agent.execute("cloneCollection", params);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("must differ")) {
        params.addProperty("target_collection", sourceName + "_copy");
        agent.execute("cloneCollection", params);
    } else { throw e; }
}

Prevention

When it happens

Trigger: Calling the cloneCollection tool/action with params where source_collection and target_collection are identical strings (e.g. both "orders").

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

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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