mongodb/laravel-mongodb · error · BadMethodCallException

This method is not supported by MongoDB

Error message

This method is not supported by MongoDB

What it means

whereColumn() compares one database column against another in SQL (WHERE a = b). MongoDB has no direct equivalent for arbitrary column-to-column comparison in the Laravel builder, so the override throws BadMethodCallException. Such comparisons must be expressed via aggregation pipelines or $expr if truly needed.

Solutions

  1. Rewrite the condition comparing two fields using whereRaw-style MQL or the library's supported operators (e.g. ->where('deactivated_at', '>', new MongoDB\BSON\UTCDateTime(...)) only works for values, so prefer an aggregation $expr via raw filter)
  2. Compute one side in PHP and use a regular where() with a concrete value if feasible
  3. Use Model::raw() / aggregation pipeline for field-vs-field comparisons
  4. Restructure the document schema so the comparison is against a stored value

Example fix

// before
User::whereColumn('updated_at', '>', 'created_at')->get();

// after
User::raw(function ($c) {
    return $c->aggregate([
        ['$match' => ['$expr' => ['$gt' => ['$updated_at', '$created_at']]]],
    ]);
});
Defensive patterns

Strategy: validation

Validate before calling

if ($builder instanceof MongoDB\Laravel\Query\Builder) { /* use aggregation $expr instead of whereColumn */ }

Try / catch

try { $q->whereColumn('a', 'b'); } catch (\BadMethodCallException $e) { $q = Model::raw(...$expr pipeline...); }

Prevention

When it happens

Trigger: Calling ->whereColumn('a', 'b') (any operator variant) on a MongoDB query builder.

Common situations: Porting SQL codebases that compare columns (e.g. whereColumn('deactivated_at', '>', 'last_seen_at')) to MongoDB; ORM-agnostic query code that uses column comparisons.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of mongodb/laravel-mongodb@0634653039 (2026-09-15). Data as JSON: /api/errors/1f382c09bc4305c2. Report an issue: GitHub.

Appendix: source

Thrown at src/Query/Builder.php:1925

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function toSql()
    {
        throw new BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function toRawSql()
    {
        throw new BadMethodCallException('This method is not supported by MongoDB. Try "toMql()" instead.');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
    {
        throw new BadMethodCallException('This method is not supported by MongoDB');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function whereFullText($columns, $value, array $options = [], $boolean = 'and')
    {
        throw new BadMethodCallException('This method is not supported by MongoDB');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]
    public function groupByRaw($sql, array $bindings = [])
    {
        throw new BadMethodCallException('This method is not supported by MongoDB');
    }

    /** @internal This method is not supported by MongoDB. */
    #[Override]

View on GitHub (pinned to 0634653039)