mongodb/mongoid · error · Mongoid::Errors::InvalidElemMatchOperator
Invalid $elemMatch operator
Error message
Invalid $elemMatch operator '%{operator}'. What it means
Raised while validating an $elemMatch condition whose keys include a '$'-prefixed operator that is neither a recognized expression operator nor a recognized field operator. Validation happens even when no matching is performed (empty value array).
Solutions
- Use only valid MongoDB query operators inside $elemMatch
- Check the operator name for typos, e.g. '$eqq' instead of '$eq'
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at lib/mongoid/matcher/elem_match.rb:40 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/65d6e765183e3ef8.
Report an issue: GitHub.
Appendix: source
Thrown at lib/mongoid/matcher/elem_match.rb:40
if value.is_a?(Array) && !value.empty?
value.any? do |v|
ElemMatchExpression.matches?(v, condition)
end
else
# Validate the condition is valid, even though we will never attempt
# matching it.
condition.each do |k, _v|
k = k.to_s
next unless k.start_with?('$')
begin
ExpressionOperator.get(k)
rescue Mongoid::Errors::InvalidExpressionOperator
begin
FieldOperator.get(k)
rescue Mongoid::Errors::InvalidFieldOperator => exc
raise Mongoid::Errors::InvalidElemMatchOperator.new(exc.operator)
end
end
end
false
end
end
end
end
end
View on GitHub (pinned to 0da0e23d71)