Class: Mongoid::Criteria::Queryable::Pipeline

Inherits:
Array
  • Object
show all
Defined in:
build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb

Overview

Represents an aggregation pipeline.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aliases = {}) {|_self| ... } ⇒ Pipeline

Initialize the new pipeline.

Examples:

Initialize the new pipeline.

Queryable::Pipeline.new(aliases)

Parameters:

  • aliases (Hash) (defaults to: {})

    A hash of mappings from aliases to the actual field names in the database.

Yields:

  • (_self)

Yield Parameters:



47
48
49
50
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 47

def initialize(aliases = {})
  @aliases = aliases
  yield(self) if block_given?
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



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

def aliases
  @aliases
end

#aliases The field aliases.(Thefieldaliases.) ⇒ Object (readonly)



11
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 11

attr_reader :aliases

Instance Method Details

#__deep_copy__Pipeline

Deep copy the aggregation pipeline. Will clone all the values in the pipeline as well as the pipeline itself.

Examples:

Deep copy the pipeline.

pipeline.__deep_copy__

Returns:



20
21
22
23
24
25
26
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 20

def __deep_copy__
  self.class.new(aliases) do |copy|
    each do |entry|
      copy.push(entry.__deep_copy__)
    end
  end
end

#group(entry) ⇒ Pipeline

Add a group operation to the aggregation pipeline.

Examples:

Add a group operation.

pipeline.group(:_id => "foo", :count.sum => 1, :max.max => "likes")

Parameters:

  • entry (Hash)

    The group entry.

Returns:



36
37
38
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 36

def group(entry)
  push("$group" => evolve(entry.__expand_complex__))
end

#project(entry) ⇒ Pipeline

Adds a $project entry to the aggregation pipeline.

Examples:

Add the projection.

pipeline.project(name: 1)

Parameters:

  • entry (Hash)

    The projection.

Returns:



60
61
62
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 60

def project(entry)
  push("$project" => evolve(entry))
end

#unwind(field_or_doc) ⇒ Pipeline

Add the $unwind entry to the pipeline.

Examples:

Add the unwind.

pipeline.unwind(:field)
pipeline.unwind(document)

Parameters:

  • field_or_doc (String, Symbol, Hash)

    A field name or a document.

Returns:



74
75
76
77
78
79
80
81
82
# File 'build/mongoid-7.4/lib/mongoid/criteria/queryable/pipeline.rb', line 74

def unwind(field_or_doc)
  unless field_or_doc.respond_to? :keys
    normalized = field_or_doc.to_s
    name = aliases[normalized] || normalized
    push("$unwind" => name.__mongo_expression__)
  else
    push("$unwind" => field_or_doc)
  end
end