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

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

Overview

Provides a DSL around crafting aggregation framework commands.

Since:

  • 2.0.0

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 'build/mongoid-7.0/lib/mongoid/criteria/queryable/aggregable.rb', line 16

attr_writer :aggregating

#aggregating=(value) ⇒ Object (writeonly)

Since:

  • 2.0.0



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

def aggregating=(value)
  @aggregating = value
end

#pipelineObject (readonly)

Since:

  • 2.0.0



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

def pipeline
  @pipeline
end

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



13
# File 'build/mongoid-7.0/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.

Since:

  • 2.0.0



27
28
29
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/aggregable.rb', line 27

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:

Since:

  • 2.0.0



44
45
46
47
48
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/aggregable.rb', line 44

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:

Since:

  • 2.0.0



68
69
70
71
72
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/aggregable.rb', line 68

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:

Since:

  • 2.0.0



84
85
86
87
88
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/aggregable.rb', line 84

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