t8y2/dbx · error · java.lang.IllegalArgumentException

dropIndexes only accepts arrays of string index names

Error message

dropIndexes only accepts arrays of string index names

What it means

When dropIndexes receives a JSON array, every element must be a non-blank JSON string index name. This error means at least one array element was not a string (e.g. an object or number) or was blank/whitespace. dropIndexes arrays cannot contain index-key specification objects.

Source

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

        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");
        }
        throw new IllegalArgumentException("dropIndexes only accepts a string index name, JSON document, or string array");
    }

    private static boolean isDefaultIdIndexSpecification(Document specification) {

View on GitHub (pinned to c0390bff16)

Solutions

  1. Ensure every element of the array is a plain string index name (use listIndexes to get the names)
  2. Remove empty or whitespace-only strings from the array
  3. To drop by key specification, resolve the actual index name via listIndexes first, then drop by that name

Example fix

// before
agent.dropIndexes("[\"email_idx\", {\"age\": 1}, \"\"]");
// after
agent.dropIndexes("[\"email_idx\", \"age_1\"]");
Defensive patterns

Strategy: validation

Validate before calling

function assertDropIndexesArray(names) {
  if (!Array.isArray(names) || names.length === 0) return;
  for (const n of names) {
    if (typeof n !== 'string' || n.trim() === '')
      throw new Error('dropIndexes array must contain only non-empty string index names, got: ' + JSON.stringify(n));
  }
}

Type guard

const isStringNameArray = (v) =>
  Array.isArray(v) && v.length > 0 && v.every(n => typeof n === 'string' && n.trim() !== '');

Try / catch

try {
  agent.dropIndexes(json);
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("dropIndexes only accepts arrays of string index names")) {
    const names = await resolveIndexNamesFromListIndexes();
    agent.dropIndexes(JSON.stringify(names.filter(n => typeof n === 'string' && n)));
  } else throw e;
}

Prevention

When it happens

Trigger: Calling dropIndexes with an array containing non-string or blank elements, e.g. '["email_idx", {"age": 1}]', '["email_idx", ""]', or '[123]'. The forEach lambda throws on the first offending element.

Common situations: Mixing index names and index-key documents in one dropIndexes call; building the JSON array programmatically where empty/null strings slip in; users assuming dropIndexes accepts key-spec objects like dropIndex does.

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/b3f833da652648b8. Report an issue: GitHub.