t8y2/dbx · error · java.lang.IllegalArgumentException

dropIndex only accepts a string index name or JSON document;

Error message

dropIndex only accepts a string index name or JSON document; arrays are not supported

What it means

This error comes from the index-specification parser shared by dropIndex/dropIndexes in MongoAgent. When the 'single' flag is set (dropIndex), the parsed JSON argument must be either a string index name or a JSON-object key/direction specification. An array was passed instead, which dropIndex does not accept; dropIndexes is the multi-name API that accepts arrays.

Source

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

            if (DEFAULT_ID_INDEX_NAME.equals(name)) {
                throw new IllegalArgumentException("The default MongoDB _id_ index cannot be dropped");
            }
            return name;
        }
        if (value.isJsonObject()) {
            JsonObject object = value.getAsJsonObject();
            if (object.size() == 0) {
                throw new IllegalArgumentException("Index specification is required");
            }
            Document specification = Document.parse(indexesJson);
            if (isDefaultIdIndexSpecification(specification)) {
                throw new IllegalArgumentException("The default MongoDB _id_ index cannot be dropped");
            }
            return specification;
        }
        if (value.isJsonArray()) {
            if (single) {
                throw new IllegalArgumentException("dropIndex only accepts a string index name or JSON document; arrays are not supported");
            }
            List<String> names = new ArrayList<>();
            value.getAsJsonArray().forEach(item -> {
                if (!item.isJsonPrimitive() || !item.getAsJsonPrimitive().isString() || item.getAsString().isBlank()) {
                    throw new IllegalArgumentException("dropIndexes only accepts arrays of string index names");
                }
                names.add(item.getAsString());
            });
            if (names.isEmpty()) {
                throw new IllegalArgumentException("dropIndexes only accepts non-empty string arrays");
            }
            if (names.contains(DEFAULT_ID_INDEX_NAME)) {
                throw new IllegalArgumentException("The default MongoDB _id_ index cannot be dropped");
            }
            return names;
        }
        if (single) {
            throw new IllegalArgumentException("dropIndex only accepts a string index name or JSON document");

View on GitHub (pinned to c0390bff16)

Solutions

  1. Pass a single index name string to dropIndex, e.g. dropIndex("users_email_idx")
  2. If you have multiple indexes to drop, call dropIndexes with the array instead of dropIndex
  3. If the index is described by a key/direction spec, pass a JSON object like {"email": 1} rather than an array

Example fix

// before
agent.dropIndex("[\"email_idx\", \"name_idx\"]");
// after
agent.dropIndex("email_idx");
// or for multiple:
agent.dropIndexes("[\"email_idx\", \"name_idx\"]");
Defensive patterns

Strategy: validation

Validate before calling

// JS-ish caller-side check before calling dropIndex
function assertDropIndexArg(arg) {
  if (typeof arg === 'string' && arg.trim() !== '') return;      // name OK
  if (typeof arg === 'object' && arg !== null && !Array.isArray(arg)
      && Object.keys(arg).length > 0) return;                     // key spec OK
  throw new Error('dropIndex requires a non-empty string name or a JSON object spec, not an array');
}

Type guard

function isDropIndexSpec(v) {
  return (typeof v === 'string' && v.trim() !== '') ||
         (v !== null && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length > 0);
}

Try / catch

try {
  agent.dropIndex(arg);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("arrays are not supported")) {
    agent.dropIndexes(arrayJson); // route arrays to dropIndexes
  } else throw e;
}

Prevention

When it happens

Trigger: Calling dropIndex with a JSON array argument such as '[["idx_a","idx_b"]]' or '["idx_a"]', instead of a string name ('idx_a') or an object ('{"field": 1}'). The parser detects a JSON array value and immediately rejects it because arrays are only valid for dropIndexes.

Common situations: Developers reusing code written for dropIndexes (which takes arrays of names) and calling dropIndex with the same array; scripting layers that build arguments dynamically and pass a list even when only one index is targeted; confusion after a driver upgrade that tightened argument validation.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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