Module: Mongoid::Positional

Included in:
Association::Embedded::Batchable, Contextual::Memory, Persistable
Defined in:
lib/mongoid/positional.rb

Overview

This module is responsible for taking update selectors and switching out the indexes for the $ positional operator where appropriate.

Instance Method Summary collapse

Instance Method Details

#positionally(selector, operations, processed = {}) ⇒ Hash

Note:

The only time we can accurately know when to use the positional operator is at the exact time we are going to persist something. So we can tell by the selector that we are sending if it is actually possible to use the positional operator at all. For example, if the selector is: { “_id” => 1 }, then we could not use the positional operator for updating embedded documents since there would never be a match - we base whether we can based on the number of levels deep the selector goes, and if the id values are not nil.

Takes the provided selector and atomic operations and replaces the indexes of the embedded documents with the positional operator when needed.

Examples:

Process the operations.

positionally(
  { "_id" => 1, "addresses._id" => 2 },
  { "$set" => { "addresses.0.street" => "hobrecht" }}
)

Parameters:

  • selector (Hash)

    The selector.

  • operations (Hash)

    The update operations.

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

    The processed update operations.

Returns:

  • (Hash)

    The new operations.



34
35
36
37
38
39
40
41
# File 'lib/mongoid/positional.rb', line 34

def positionally(selector, operations, processed = {})
  if selector.size == 1 || selector.values.any? { |val| val.nil? }
    return operations
  end
  keys = selector.keys.map{ |m| m.sub('._id','') } - ['_id']
  keys = keys.sort_by { |s| s.length*-1 }
  process_operations(keys, operations, processed)
end