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

Extended by:
Macroable
Included in:
Mongoid::Criteria::Queryable
Defined in:
lib/mongoid/criteria/queryable/aggregable.rb

Overview

Provides a DSL around crafting aggregation framework commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Macroable

key

Instance Attribute Details

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



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

attr_writer :aggregating

#aggregating=(value) ⇒ Object (writeonly)

Sets the attribute aggregating

Parameters:

  • value

    the value to set the attribute aggregating to.



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

def aggregating=(value)
  @aggregating = value
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



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

def pipeline
  @pipeline
end

#pipeline The aggregation pipeline.(Theaggregationpipeline.) ⇒ Object (readonly)



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

attr_reader :pipeline

Instance Method Details

#aggregating?true | false

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.



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

def aggregating?
  !!@aggregating
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:



40
41
42
43
44
# 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:



62
63
64
65
66
# 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:



76
77
78
79
80
# File 'lib/mongoid/criteria/queryable/aggregable.rb', line 76

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