Module: MongoQL::CollectionOperators

Included in:
Expression
Defined in:
lib/mongo_ql/collection_operators.rb

Constant Summary collapse

AGGREGATE_OPS =
{
  "max":   "$max",
  "min":   "$min",
  "first": "$first",
  "last":  "$last",
  "sum":   "$sum",
  "avg":   "$avg",
  "size":  "$size",
  "push":  "$push",
  "reverse": "$reverseArray"
}.freeze

Instance Method Summary collapse

Instance Method Details

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mongo_ql/collection_operators.rb', line 74

def any?(&block)
  if_null([]).filter(&block).size > 0
end

#concat_arrays(*expressions) ⇒ Object



36
37
38
39
40
# File 'lib/mongo_ql/collection_operators.rb', line 36

def concat_arrays(*expressions)
  Expression::MethodCall.new "$concatArrays", self, ast_template: -> (target, **_args) {
    [target, *expressions]
  }
end

#contains(ele) ⇒ Object Also known as: includes, include, include?, includes?



64
65
66
67
68
# File 'lib/mongo_ql/collection_operators.rb', line 64

def contains(ele)
  Expression::MethodCall.new "$in", self, ast_template: -> (target, **_args) {
    [to_expression(ele), target]
  }
end

#filter(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/mongo_ql/collection_operators.rb', line 25

def filter(&block)
  evaled_cond = block.call(Expression::FieldNode.new("$item"))
  Expression::MethodCall.new "$filter", self, ast_template: -> (target, **_args) {
    {
      "input" => target,
      "as"    => "item",
      "cond"  => evaled_cond
    }
  }
end

#map(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/mongo_ql/collection_operators.rb', line 42

def map(&block)
  evaled_in = block.call(Expression::FieldNode.new("$item"))
  Expression::MethodCall.new "$map", self, ast_template: -> (target, **_args) {
    {
      "input" => target,
      "as"    => "item",
      "in"    => evaled_in
    }
  }
end

#reduce(initial_value, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/mongo_ql/collection_operators.rb', line 53

def reduce(initial_value, &block)
  evaled_in = to_expression(block.call(Expression::FieldNode.new("$value"), Expression::FieldNode.new("$this")))
  Expression::MethodCall.new "$reduce", self, ast_template: -> (target, **_args) {
    {
      "input"        => target,
      "initialValue" => to_expression(initial_value),
      "in"           => evaled_in
    }
  }
end