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

Invalid $elemMatch operator '%{operator}'.

Error message

Invalid $elemMatch operator '%{operator}'.

What it means

Error "Invalid $elemMatch operator '%{operator}'." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/matcher/elem_match.rb:40

        if value.is_a?(Array) && !value.empty?
          value.any? do |v|
            ElemMatchExpression.matches?(v, condition)
          end
        else
          # Validate the condition is valid, even though we will never attempt
          # matching it.
          condition.each do |k, _v|
            k = k.to_s
            next unless k.start_with?('$')

            begin
              ExpressionOperator.get(k)
            rescue Mongoid::Errors::InvalidExpressionOperator
              begin
                FieldOperator.get(k)
              rescue Mongoid::Errors::InvalidFieldOperator => exc
                raise Mongoid::Errors::InvalidElemMatchOperator.new(exc.operator)
              end
            end
          end
          false
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/matcher/elem_match.rb:40 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/65d6e765183e3ef8. Report an issue: GitHub.