Module: Mongoid::Matcher::ElemMatch Private

Defined in:
build/mongoid-7.3/lib/mongoid/matcher/elem_match.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.matches?(exists, value, condition) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'build/mongoid-7.3/lib/mongoid/matcher/elem_match.rb', line 6

module_function def matches?(exists, value, condition)
  unless Hash === condition
    raise Errors::InvalidQuery, "$elemMatch requires a Hash operand: #{Errors::InvalidQuery.truncate_expr(condition)}"
  end
  if Array === value && !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
      if 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
    end
    false
  end
end