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

$or argument must be an array: #

Error message

$or argument must be an array: #{Errors::InvalidQuery.truncate_expr(expr)}

What it means

Raised by Mongoid::Matcher::Or.matches? when the $or expression is not an Array. MongoDB requires $or to be an array of condition hashes; the faulting input is the non-Array expr (truncated in the message).

Solutions

  1. Pass an Array of Hash conditions: { '$or' => [{ a: 1 }, { b: 2 }] }
  2. Use plain field conditions instead of $or for single-clause logic
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at lib/mongoid/matcher/or.rb:19 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/98538849afa5e55a. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/matcher/or.rb:19

module Mongoid
  module Matcher
    # In-memory matcher for $or expression.
    #
    # @see https://www.mongodb.com/docs/manual/reference/operator/query/or/
    #
    # @api private
    module Or
      # Returns whether a document satisfies an $or expression.
      #
      # @param [ Mongoid::Document ] document The document.
      # @param [ Array<Hash> ] expr The $or conditions.
      #
      # @return [ true | false ] Whether the document matches.
      #
      # @api private
      module_function def matches?(document, expr)
        unless expr.is_a?(Array)
          raise Errors::InvalidQuery, "$or argument must be an array: #{Errors::InvalidQuery.truncate_expr(expr)}"
        end

        if expr.empty?
          raise Errors::InvalidQuery, "$or argument must be a non-empty array: #{Errors::InvalidQuery.truncate_expr(expr)}"
        end

        expr.any? do |sub_expr|
          Expression.matches?(document, sub_expr)
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)