class Mongoid::Criteria::Queryable::Pipeline

Represents an aggregation pipeline.

@since 2.0.0

Attributes

aliases[R]

@attribute [r] aliases The field aliases.

Public Class Methods

new(aliases = {}) { |self| ... } click to toggle source

Initialize the new pipeline.

@example Initialize the new pipeline.

Queryable::Pipeline.new(aliases)

@param [ Hash ] aliases A hash of mappings from aliases to the actual

field names in the database.

@since 2.0.0

# File lib/mongoid/criteria/queryable/pipeline.rb, line 56
def initialize(aliases = {})
  @aliases = aliases
  yield(self) if block_given?
end

Public Instance Methods

__deep_copy__() click to toggle source

Deep copy the aggregation pipeline. Will clone all the values in the pipeline as well as the pipeline itself.

@example Deep copy the pipeline.

pipeline.__deep_copy__

@return [ Pipeline ] The cloned pipeline.

@since 2.0.0

# File lib/mongoid/criteria/queryable/pipeline.rb, line 25
def __deep_copy__
  self.class.new(aliases) do |copy|
    each do |entry|
      copy.push(entry.__deep_copy__)
    end
  end
end
group(entry) click to toggle source

Add a group operation to the aggregation pipeline.

@example Add a group operation.

pipeline.group(:_id => "foo", :count.sum => 1, :max.max => "likes")

@param [ Hash ] entry The group entry.

@return [ Pipeline ] The pipeline.

@since 2.0.0

# File lib/mongoid/criteria/queryable/pipeline.rb, line 43
def group(entry)
  push("$group" => evolve(entry.__expand_complex__))
end
project(entry) click to toggle source

Adds a $project entry to the aggregation pipeline.

@example Add the projection.

pipeline.project(name: 1)

@param [ Hash ] entry The projection.

@return [ Pipeline ] The pipeline.

# File lib/mongoid/criteria/queryable/pipeline.rb, line 69
def project(entry)
  push("$project" => evolve(entry))
end
unwind(field_or_doc) click to toggle source

Add the $unwind entry to the pipeline.

@example Add the unwind.

pipeline.unwind(:field)
pipeline.unwind(document)

@param [ String, Symbol, Hash ] field_or_doc A field name or a

document.

@return [ Pipeline ] The pipeline.

@since 2.0.0

# File lib/mongoid/criteria/queryable/pipeline.rb, line 85
def unwind(field_or_doc)
  unless field_or_doc.respond_to? :keys
    normalized = field_or_doc.to_s
    name = aliases[normalized] || normalized
    push("$unwind" => name.__mongo_expression__)
  else
    push("$unwind" => field_or_doc)
  end
end

Private Instance Methods

evolve(entry) click to toggle source

Evolve the entry using the aliases.

@api private

@example Evolve the entry.

pipeline.evolve(name: 1)

@param [ Hash ] entry The entry to evolve.

@return [ Hash ] The evolved entry.

@since 2.0.0

# File lib/mongoid/criteria/queryable/pipeline.rb, line 109
def evolve(entry)
  aggregate = Selector.new(aliases)
  entry.each_pair do |field, value|
    aggregate.merge!(field.to_s => value)
  end
  aggregate
end