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

'$ne' operator does not allow Regexp arguments: #

Error message

'$ne' operator does not allow Regexp arguments: #{Errors::InvalidQuery.truncate_expr(condition)}

What it means

Raised by Mongoid::Matcher::Ne.matches? when the $ne condition is a Regexp. MongoDB does not support negated regex matching via $ne with a regex predicate in this matcher, so regexp arguments are rejected; the faulting input is the Regexp condition.

Solutions

  1. Use { field: { '$not' => /pattern/ } } for negated regex matching
  2. Compare against a literal value with $ne instead of a pattern
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at lib/mongoid/matcher/ne.rb:21 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/e7087efe4039cf0f. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/matcher/ne.rb:21

    # In-memory matcher for $ne expression.
    #
    # @see https://www.mongodb.com/docs/manual/reference/operator/query/ne/
    #
    # @api private
    module Ne
      # Returns whether a value satisfies an $ne expression.
      #
      # @param [ true | false ] exists Whether the value exists.
      # @param [ Object ] value The value to check.
      # @param [ Object ] condition The $ne condition predicate.
      #
      # @return [ true | false ] Whether the value matches.
      #
      # @api private
      module_function def matches?(exists, value, condition)
        case condition
        when ::Regexp, BSON::Regexp::Raw
          raise Errors::InvalidQuery, "'$ne' operator does not allow Regexp arguments: #{Errors::InvalidQuery.truncate_expr(condition)}"
        end

        !EqImpl.matches?(exists, value, condition, '$ne')
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)