t8y2/dbx · error · IllegalArgumentException

MongoDB aggregate limit must be between 0 and ${Integer.MAX_

Error message

MongoDB aggregate limit must be between 0 and ${Integer.MAX_VALUE}

What it means

aggregateMaxRows reads the top-level 'limit' parameter as a long and requires 0 <= limit <= Integer.MAX_VALUE because it is narrowed to an int for the result cap. Out-of-range values (negative, or above ~2.147e9) throw IllegalArgumentException before the aggregation runs.

Source

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

            "maxTimeMS",
            "maxAwaitTimeMS",
            "bypassDocumentValidation",
            "collation",
            "comment",
            "hint",
            "useCursor"
        );
        for (String key : options.keySet()) {
            if (!supported.contains(key)) {
                throw new IllegalArgumentException("Unsupported MongoDB Legacy aggregate option: " + key);
            }
        }
    }

    private static int aggregateMaxRows(JsonObject params) {
        long value = params.has("limit") ? params.get("limit").getAsLong() : 100;
        if (value < 0 || value > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("MongoDB aggregate limit must be between 0 and " + Integer.MAX_VALUE);
        }
        return (int) value;
    }

    private static boolean aggregateBoolean(Document options, String key) {
        Object value = options.get(key);
        if (!(value instanceof Boolean)) {
            throw new IllegalArgumentException("MongoDB aggregate option " + key + " must be a boolean");
        }
        return (Boolean) value;
    }

    private static int aggregateNonNegativeInt(Document options, String key) {
        long value = aggregateNonNegativeLong(options, key);
        if (value > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("MongoDB aggregate option " + key + " is too large");
        }
        return (int) value;

View on GitHub (pinned to c0390bff16)

Solutions

  1. Set limit to a non-negative int-range value, e.g. 0–2147483647; 0 or omission defaults to the built-in 100.
  2. Clamp on the caller side: Math.max(0, Math.min(value, Integer.MAX_VALUE)).
  3. If you need effectively unbounded output, paginate with cursor batches instead of a huge limit.
  4. Validate/sanitize the limit field before constructing the request.

Example fix

// before
long limit = -1; // meant 'unlimited'
// after
int limit = (int) Math.max(0, Math.min(rawLimit, Integer.MAX_VALUE));
Defensive patterns

Strategy: validation

Validate before calling

long limit = params.has("limit") ? params.get("limit").getAsLong() : 100;
if (limit < 0 || limit > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("limit must be in [0, 2147483647]");
}

Type guard

static boolean isValidLimit(long value) {
    return value >= 0 && value <= Integer.MAX_VALUE;
}

Try / catch

try {
    agent.aggregate(db, collection, pipeline, options);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("MongoDB aggregate limit must be between")) {
        // clamp and retry, or reject the request with a typed validation error
        params.addProperty("limit", Math.max(0, params.get("limit").getAsLong()));
    } else throw e;
}

Prevention

When it happens

Trigger: Calling aggregate with {"limit": -1} or an absurd value like {"limit": 99999999999} (or any value exceeding 2147483647).

Common situations: Config where limit=-1 meant 'unlimited' in another system; unvalidated user input or a JSON number that overflows int; copy-paste from APIs where 0/negative signals no cap.

Related errors


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