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

$or argument must be a non-empty array: #

Error message

$or argument must be a non-empty array: #{Errors::InvalidQuery.truncate_expr(expr)}

What it means

Raised inside Mongoid::Matcher::Or.matches? when the expr passed for an $or clause is an Array but contains zero elements. This guard is a plain emptiness check on a generic input: the method first type-checks that expr is an Array, then requires at least one element, because the match is computed as expr.any? { ... } over the sub-expressions — with no sub-expressions there is nothing to satisfy, and MongoDB likewise rejects an empty $or array ("$or/$and/$nor must be a nonempty array"). The input at fault is the expr argument, typically a programmatically built condition list that ended up empty, e.g. {"$or" => []}.

Solutions

  1. Include at least one condition Hash in the $or array
  2. Omit $or entirely when no alternative conditions are needed
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/matcher/or.rb:23 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/11eaa97cd9d8d119. Report an issue: GitHub.

Appendix: source

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

    # @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)