123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Criteria::Queryable::Pipeline

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Array
  • Object
Defined in: lib/mongoid/criteria/queryable/pipeline.rb

Overview

Represents an aggregation pipeline.

Class Attribute Summary

::Mongoid::Extensions::Array::ClassMethods - Extended

resizable?

Returns whether the object’s size can be changed.

Class Method Summary

::Mongoid::Extensions::Array::ClassMethods - Extended

__mongoize_fk__

Convert the provided object to a proper array of foreign keys.

mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

Instance Attribute Summary

::Mongoid::Extensions::Array - Included

#multi_arged?

Is the array a set of multiple arguments in a method?

#resizable?

Returns whether the object’s size can be changed.

Instance Method Summary

::Mongoid::Extensions::Array - Included

#__evolve_object_id__

Evolve the array into an array of object ids.

#__find_args__

Get the array of args as arguments for a find query.

#__mongoize_object_id__

Mongoize the array into an array of object ids.

#__mongoize_time__

Converts the array for storing as a time.

#delete_one

Delete the first object in the array that is equal to the supplied object and return it.

#mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

Constructor Details

.new(aliases = {}) {|_self| ... } ⇒ Pipeline

Initialize the new pipeline.

Examples:

Initialize the new pipeline.

Queryable::Pipeline.new(aliases)

Parameters:

  • aliases (Hash) (defaults to: {})

    A hash of mappings from aliases to the actual field names in the database.

Yields:

  • (_self)

Yield Parameters:

  • _self (Pipeline)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 48

def initialize(aliases = {})
  @aliases = aliases
  yield(self) if block_given?
end

Instance Attribute Details

#aliases (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 12

attr_reader :aliases

#aliases The field aliases.(The field aliases.) (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 12

attr_reader :aliases

Instance Method Details

#__deep_copy__Pipeline

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

Examples:

Deep copy the pipeline.

pipeline.__deep_copy__

Returns:

  • (Pipeline)

    The cloned pipeline.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 21

def __deep_copy__
  self.class.new(aliases) do |copy|
    each do |entry|
      copy.push(entry.__deep_copy__)
    end
  end
end

#evolve(entry) ⇒ Hash (private)

This method is for internal use only.

Evolve the entry using the aliases.

Examples:

Evolve the entry.

pipeline.evolve(name: 1)

Parameters:

  • entry (Hash)

    The entry to evolve.

Returns:

  • (Hash)

    The evolved entry.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 97

def evolve(entry)
  aggregate = Selector.new(aliases)
  entry.each_pair do |field, value|
    aggregate.merge!(field.to_s => value)
  end
  aggregate
end

#group(entry) ⇒ Pipeline

Add a group operation to the aggregation pipeline.

Examples:

Add a group operation.

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

Parameters:

  • entry (Hash)

    The group entry.

Returns:

  • (Pipeline)

    The pipeline.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 37

def group(entry)
  push("$group" => evolve(entry.__expand_complex__))
end

#project(entry) ⇒ Pipeline

Adds a $project entry to the aggregation pipeline.

Examples:

Add the projection.

pipeline.project(name: 1)

Parameters:

  • entry (Hash)

    The projection.

Returns:

  • (Pipeline)

    The pipeline.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 61

def project(entry)
  push("$project" => evolve(entry))
end

#unwind(field_or_doc) ⇒ Pipeline

Add the $unwind entry to the pipeline.

Examples:

Add the unwind.

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

Parameters:

Returns:

  • (Pipeline)

    The pipeline.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/pipeline.rb', line 75

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