mongodb/mongoid · error · Mongoid::Errors::InvalidQuery

$regex requires a regular expression argument: #{Errors::Inv

Error message

$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}

What it means

Error "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}" thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/matcher/regex.rb:27

      # Returns whether a value satisfies a $regex expression.
      #
      # @param [ true | false ] exists Not used.
      # @param [ String | Array<String> ] value The value to check.
      # @param [ Regexp | BSON::Regexp::Raw ] condition The $regex condition.
      #
      # @return [ true | false ] Whether the value matches.
      #
      # @api private
      module_function def matches?(_exists, value, condition)
        condition = case condition
                    when Regexp
                      condition
                    when BSON::Regexp::Raw
                      condition.compile
                    else
                      # Note that strings must have been converted to a regular expression
                      # instance already (with $options taken into account, if provided).
                      raise Errors::InvalidQuery, "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}"
                    end

        case value
        when Array
          value.any? do |v|
            v =~ condition
          end
        when String
          value =~ condition
        else
          false
        end
      end

      # Returns whether an scalar or array value matches a Regexp.
      #
      # @param [ true | false ] exists Not used.
      # @param [ String | Array<String> ] value The value to check.

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/matcher/regex.rb:27 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/e1b24be9363d8b31. Report an issue: GitHub.