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

Malformed $mod argument #

Error message

Malformed $mod argument #{condition}, should have 2 elements

What it means

Raised by Mongoid::Matcher::Mod.matches? when the $mod condition is not a two-element [divisor, remainder] array. MongoDB's $mod requires exactly those two numeric operands; the faulting input is the malformed condition array.

Solutions

  1. Use exactly [divisor, remainder] as the $mod argument
  2. Remove extra elements from the array
Defensive patterns

Strategy: validation

When it happens

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

Common situations: See trigger scenarios.

Understand the failure class


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

Appendix: source

Thrown at lib/mongoid/matcher/mod.rb:22

    #
    # @see https://www.mongodb.com/docs/manual/reference/operator/query/mod/
    #
    # @api private
    module Mod
      # Returns whether a value satisfies a $mod expression.
      #
      # @param [ true | false ] exists Not used.
      # @param [ Numeric ] value The value to check.
      # @param [ Array<Numeric> ] condition The $mod condition predicate,
      #   which is a 2-tuple containing the divisor and remainder.
      #
      # @return [ true | false ] Whether the value matches.
      #
      # @api private
      module_function def matches?(_exists, value, condition)
        raise Errors::InvalidQuery, "Unknown $mod argument #{condition}" unless condition.is_a?(Array)
        if condition.length != 2
          raise Errors::InvalidQuery, "Malformed $mod argument #{condition}, should have 2 elements"
        end

        condition[1] == value % condition[0]
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)