123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Criteria::Queryable::Aggregable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macroable
Defined in: lib/mongoid/criteria/queryable/aggregable.rb

Overview

Provides a DSL around crafting aggregation framework commands.

Class Method Summary

Macroable - Extended

key

Adds a method on ::Symbol for convenience in where queries for the provided operators.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#aggregating Flag for whether or not we are aggregating.(Flag for whether or not we are aggregating.) (rw)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 16

attr_writer :aggregating

#aggregating=(value) (rw)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 16

attr_writer :aggregating

#aggregating?true | false (rw)

Has the aggregable enter an aggregation state. Ie, are only aggregation operations allowed at this point on.

Examples:

Is the aggregable aggregating?

aggregable.aggregating?

Returns:

  • (true | false)

    If the aggregable is aggregating.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 25

def aggregating?
  !!@aggregating
end

#pipeline (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 13

attr_reader :pipeline

#pipeline The aggregation pipeline.(The aggregation pipeline.) (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 13

attr_reader :pipeline

Instance Method Details

#aggregation(operation) ⇒ Aggregable (private)

This method is for internal use only.

Add the aggregation operation.

Examples:

Aggregate on the operation.

aggregation(operation) do |pipeline|
  pipeline.push("$project" => operation)
end

Parameters:

  • operation (Hash)

    The operation for the pipeline.

Returns:

  • (Aggregable)

    The cloned aggregable.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 96

def aggregation(operation)
  return self unless operation
  clone.tap do |query|
    unless aggregating?
      query.pipeline.concat(query.selector.to_pipeline)
      query.pipeline.concat(query.options.to_pipeline)
      query.aggregating = true
    end
    yield(query.pipeline)
  end
end

#group(operation) ⇒ Aggregable

Add a group ($group) operation to the aggregation pipeline.

Examples:

Add a group operation being verbose.

aggregable.group(count: { "$sum" => 1 }, max: { "$max" => "likes" })

Add a group operation using symbol shortcuts.

aggregable.group(:count.sum => 1, :max.max => "likes")

Parameters:

  • operation (Hash)

    The group operation.

Returns:

  • (Aggregable)

    The aggregable.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 40

def group(operation)
  aggregation(operation) do |pipeline|
    pipeline.group(operation)
  end
end

#project(operation = nil) ⇒ Aggregable

Add a projection ($project) to the aggregation pipeline.

Examples:

Add a projection to the pipeline.

aggregable.project(author: 1, name: 0)

Parameters:

  • operation (Hash) (defaults to: nil)

    The projection to make.

Returns:

  • (Aggregable)

    The aggregable.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 62

def project(operation = nil)
  aggregation(operation) do |pipeline|
    pipeline.project(operation)
  end
end

#unwind(field) ⇒ Aggregable

Add an unwind ($unwind) to the aggregation pipeline.

Examples:

Add an unwind to the pipeline.

aggregable.unwind(:field)

Parameters:

  • field (String | Symbol)

    The name of the field to unwind.

Returns:

  • (Aggregable)

    The aggregable.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 76

def unwind(field)
  aggregation(field) do |pipeline|
    pipeline.unwind(field)
  end
end