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

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



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

attr_writer :aggregating

#aggregating=(value) ⇒ Object (writeonly)

Since:

  • 2.0.0



18
19
20
# File 'build/mongoid-7.3/lib/mongoid/criteria/queryable/aggregable.rb', line 18

def aggregating=(value)
  @aggregating = value
end

#pipelineObject (readonly)

Since:

  • 2.0.0



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

def pipeline
  @pipeline
end

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



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

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



29
30
31
# File 'build/mongoid-7.3/lib/mongoid/criteria/queryable/aggregable.rb', line 29

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



46
47
48
49
50
# File 'build/mongoid-7.3/lib/mongoid/criteria/queryable/aggregable.rb', line 46

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



70
71
72
73
74
# File 'build/mongoid-7.3/lib/mongoid/criteria/queryable/aggregable.rb', line 70

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



86
87
88
89
90
# File 'build/mongoid-7.3/lib/mongoid/criteria/queryable/aggregable.rb', line 86

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