mongodb/mongoid · error · Mongoid::Errors::InvalidQuery
$and argument must be a non-empty array: #
Error message
$and argument must be a non-empty array: #{Errors::InvalidQuery.truncate_expr(expr)} What it means
Raised by the in-memory $and matcher when the $and operand is an Array but is empty. MongoDB rejects empty $and arrays, so the in-memory matcher mirrors that rule when evaluating documents client-side.
Solutions
- Include at least one condition Hash inside the $and array
- Remove the $and wrapper entirely if no conditions are needed
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at lib/mongoid/matcher/and.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/8133129f27c2d948.
Report an issue: GitHub.
Appendix: source
Thrown at lib/mongoid/matcher/and.rb:23
# @see https://www.mongodb.com/docs/manual/reference/operator/query/and/
#
# @api private
module And
# Returns whether a document satisfies an $and expression.
#
# @param [ Mongoid::Document ] document The document.
# @param [ Array<Hash> ] expr The $and conditions.
#
# @return [ true | false ] Whether the document matches.
#
# @api private
module_function def matches?(document, expr)
unless expr.is_a?(Array)
raise Errors::InvalidQuery, "$and argument must be an array: #{Errors::InvalidQuery.truncate_expr(expr)}"
end
if expr.empty?
raise Errors::InvalidQuery, "$and argument must be a non-empty array: #{Errors::InvalidQuery.truncate_expr(expr)}"
end
expr.all? do |sub_expr|
Expression.matches?(document, sub_expr)
end
end
end
end
end
View on GitHub (pinned to 0da0e23d71)