Module: Mongoid::Shardable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
lib/mongoid/shardable.rb

Overview

This module contains behavior for adding shard key fields to updates.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#shard_key_field_value(field, prefer_persisted:) ⇒ Object

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 value for the named shard key. If the field identifies an embedded document, the key will be parsed and recursively evaluated. If ‘prefer_persisted` is true, the value last persisted to the database will be returned, regardless of what the current value of the attribute may be.

Parameters:

  • field (String)

    The name of the field to evaluate

  • prefer_persisted (true|false)

    Whether or not to prefer the persisted value over the current value.

Returns:

  • (Object)

    The value of the named field.



96
97
98
99
100
101
102
103
104
105
# File 'lib/mongoid/shardable.rb', line 96

def shard_key_field_value(field, prefer_persisted:)
  if field.include?(".")
    relation, remaining = field.split(".", 2)
    send(relation)&.shard_key_field_value(remaining, prefer_persisted: prefer_persisted)
  elsif prefer_persisted && !new_record?
    attribute_was(field)
  else
    send(field)
  end
end

#shard_key_fieldsArray<String>

Note:

Refactored from using delegate for class load performance.

Get the shard key fields.

Examples:

Get the shard key fields.

model.shard_key_fields

Returns:

  • (Array<String>)

    The shard key field names.



47
48
49
# File 'lib/mongoid/shardable.rb', line 47

def shard_key_fields
  self.class.shard_key_fields
end

#shard_key_selector(prefer_persisted: false) ⇒ 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 selector that would match the defined shard keys. If ‘prefer_persisted` is false (the default), it uses the current values of the specified shard keys, otherwise, it will try to use whatever value was most recently persisted.

Parameters:

  • prefer_persisted (true | false) (defaults to: false)

    Whether to use the current value of the shard key fields, or to use their most recently persisted values.

Returns:

  • (Hash)

    The shard key selector.



63
64
65
66
67
# File 'lib/mongoid/shardable.rb', line 63

def shard_key_selector(prefer_persisted: false)
  shard_key_fields.each_with_object({}) do |field, selector|
    selector[field.to_s] = shard_key_field_value(field.to_s, prefer_persisted: prefer_persisted)
  end
end

#shard_key_selector_in_dbHash

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 selector that would match the existing version of this document in the database.

If the document is not persisted, this method uses the current values of the shard key fields. If the document is persisted, this method uses the values retrieved from the database.

Returns:

  • (Hash)

    The shard key selector.



79
80
81
# File 'lib/mongoid/shardable.rb', line 79

def shard_key_selector_in_db
  shard_key_selector(prefer_persisted: true)
end