Class: Mongoid::Association::EagerLoad::LookupPipeline Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/association/eager_load/lookup_pipeline.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builds the aggregation pipeline that eager-loads a criteria's inclusions with $lookup.

It starts with the criteria's own match/sort/skip/limit, then lets each root of the inclusion tree contribute its stages. This object owns the stage-building helpers; how each inclusion contributes is the inclusion's own business (see Inclusion).

For Band.eager_load(albums: :tracks) the result is roughly:

[ <criteria match / sort / skip / limit>,
{ '$lookup' => {                 # JoinedInclusion(:albums)
  'from' => 'albums',
  'localField' => '_id',
  'foreignField' => 'band_id',
  'as' => 'albums',
  'pipeline' => [
    { '$sort' => {
      '_id' => 1
    } },
    { '$lookup' => {             # JoinedInclusion(:tracks), nested
      'as' => 'tracks',
      'pipeline' => [
        { '$sort' => {
          '_id' => 1
        } }
      ]
    } }
  ]
} } ]

Instance Method Summary collapse

Constructor Details

#initialize(criteria) ⇒ LookupPipeline

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LookupPipeline.



42
43
44
# File 'lib/mongoid/association/eager_load/lookup_pipeline.rb', line 42

def initialize(criteria)
  @criteria = criteria
end

Instance Method Details

#distribute(association, chain, lookup_stage) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds the stages that distribute a referenced inclusion living inside an embedded document onto that document.

Parameters:

Returns:

  • (Array<Hash>)

    The stages to append.



83
84
85
# File 'lib/mongoid/association/eager_load/lookup_pipeline.rb', line 83

def distribute(association, chain, lookup_stage)
  EmbeddedDistributor.for(association: association, chain: chain, lookup_stage: lookup_stage).stages
end

#lookup_stage_for(association) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The $lookup stage for a referenced inclusion: its key fields, a discriminator match when the target shares its collection with sibling subclasses, and an order. Children are added by the inclusion itself.

Parameters:

Returns:

  • (Hash)

    The $lookup stage.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongoid/association/eager_load/lookup_pipeline.rb', line 61

def lookup_stage_for(association)
  local_field, foreign_field = lookup_fields(association)
  sub_pipeline = []
  sub_pipeline << discriminator_match(association) if association.klass.hereditary?
  sub_pipeline << order(association)
  { '$lookup' => {
    'from' => association.klass.collection.name,
    'localField' => local_field,
    'foreignField' => foreign_field,
    'as' => association.name.to_s,
    'pipeline' => sub_pipeline
  } }
end

#stagesArray<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The aggregation pipeline stages.

Returns:

  • (Array<Hash>)

    The aggregation pipeline stages.



47
48
49
50
51
52
# File 'lib/mongoid/association/eager_load/lookup_pipeline.rb', line 47

def stages
  pipeline = @criteria.selector.to_pipeline
  pipeline.concat(@criteria.options.to_pipeline_for_lookup)
  InclusionTree.from(@criteria.inclusions, self).contribute_to(pipeline)
  pipeline
end