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

mongoid.errors.messages.invalid_field_operator.message

Error message

mongoid.errors.messages.invalid_field_operator.message

What it means

Raised by FieldOperator.get when a '$'-prefixed key in a field-level condition hash is not found in the field operator MAP. It is the standard 'unknown query operator' signal for in-memory matching, resolved through the InvalidFieldOperator error class.

Solutions

  1. Use a valid field operator such as $eq, $ne, $gt, $gte, $in, $nin, $regex, $size, or $type
  2. Check for typos in the operator name
  3. Move logical combinators ($and, $or, $not) to the expression level, not inside a field condition
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/matcher/field_operator.rb:44 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/10668d49145207d1. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/matcher/field_operator.rb:44

        '$regex' => Regex,
        '$size' => Size,
        '$type' => Type
      }.freeze

      # Returns the matcher module for a given operator.
      #
      # @param [ String ] op The operator name.
      #
      # @return [ Module ] The matcher module.
      #
      # @raises [ Mongoid::Errors::InvalidFieldOperator ]
      #   Raised if the given operator is unknown.
      #
      # @api private
      module_function def get(op)
        MAP.fetch(op)
      rescue KeyError
        raise Errors::InvalidFieldOperator.new(op)
      end

      # Used for evaluating $lt, $lte, $gt, $gte comparison operators.
      #
      # @todo Refactor this as it is only relevant to $lt, $lte, $gt, $gte.
      #
      # @api private
      module_function def apply_array_field_operator(_exists, value, _condition, &block)
        if value.is_a?(Array)
          value.any?(&block)
        else
          yield value
        end
      end

      # Used for evaluating $lt, $lte, $gt, $gte comparison operators.
      #
      # @todo Refactor this as it is only relevant to $lt, $lte, $gt, $gte.

View on GitHub (pinned to 0da0e23d71)