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

Extended by:
Macroable
Included in:
Mongoid::Criteria::Queryable
Defined in:
build/mongoid-7.4/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



15
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 15

attr_writer :aggregating

#aggregating=(value) ⇒ Object (writeonly)

Sets the attribute aggregating

Parameters:

  • value

    the value to set the attribute aggregating to.



15
16
17
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 15

def aggregating=(value)
  @aggregating = value
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



12
13
14
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 12

def pipeline
  @pipeline
end

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



12
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 12

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.



24
25
26
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 24

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:



39
40
41
42
43
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 39

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:



61
62
63
64
65
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 61

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:



75
76
77
78
79
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/aggregable.rb', line 75

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