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

What it means

dropIndex expects a string index name or a JSON-object index-key specification. This error means the argument parsed as neither — e.g. a number, boolean, or null. It is the fall-through rejection after string, object, and array cases were all exhausted in the single (dropIndex) path.

Source

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

                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) {
        if (specification.size() != 1 || !specification.containsKey("_id")) {
            return false;
        }
        Object direction = specification.get("_id");
        if (direction instanceof Number number) {
            return number.doubleValue() == 1.0;
        }
        // Document.parse turns Extended JSON $numberDecimal values into
        // Decimal128, which does not implement Number in the legacy driver.
        return direction instanceof Decimal128 decimal
            && decimal.bigDecimalValue().compareTo(BigDecimal.ONE) == 0;
    }

View on GitHub (pinned to c0390bff16)

Solutions

  1. Pass the index name as a JSON string: dropIndex("my_index_name")
  2. Or pass a key specification object: dropIndex("{\"field\": 1}")
  3. Log/print the exact argument being passed and confirm it is a quoted JSON string or object

Example fix

// before
agent.dropIndex(String.valueOf(indexId)); // e.g. "123"
// after
agent.dropIndex("email_1_age_-1"); // a string index name
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof indexName !== 'string' || indexName.trim() === '') {
  throw new Error('dropIndex requires a non-empty string index name, got: ' + typeof indexName);
}

Type guard

const isIndexName = (v) => typeof v === 'string' && v.trim() !== '';

Try / catch

try {
  agent.dropIndex(String(arg));
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("dropIndex only accepts a string index name or JSON document")) {
    log.error('Bad dropIndex argument type', { arg });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling dropIndex with a non-string/non-object JSON argument such as '123', 'true', or 'null'; passing a mis-serialized value where quotes were lost so the name became an invalid JSON token; blank index names are caught earlier with a separate message, so this is the malformed-type case.

Common situations: String interpolation bugs where the index name variable is null and 'null' is passed; tooling that JSON-encodes a non-string variable; API consumers reading the index name from config and getting a wrong type (number instead of string).

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