module MongoQL::CollectionOperators

Constants

AGGREGATE_OPS

Public Instance Methods

any?(&block) click to toggle source
# File lib/mongo_ql/collection_operators.rb, line 74
def any?(&block)
  if_null([]).filter(&block).size > 0
end
concat_arrays(*expressions) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: includes, include, include?, includes?
filter(&block) click to toggle source
# 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
include(ele)
Alias for: contains
include?(ele)
Alias for: contains
includes(ele)
Alias for: contains
includes?(ele)
Alias for: contains
map(&block) click to toggle source
# 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) click to toggle source
# 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