Module: Mongoid::Shardable

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

Overview

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

Since:

  • 4.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#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.

Since:

  • 1.0.0



51
52
53
# File 'build/mongoid-7.3/lib/mongoid/shardable.rb', line 51

def shard_key_fields
  self.class.shard_key_fields
end

#shard_key_selectorHash

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 current version of this document.

Returns:

  • (Hash)

    The shard key selector.

Since:

  • 4.0.0



61
62
63
64
65
66
67
# File 'build/mongoid-7.3/lib/mongoid/shardable.rb', line 61

def shard_key_selector
  selector = {}
  shard_key_fields.each do |field|
    selector[field.to_s] = send(field)
  end
  selector
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.

Since:

  • 4.0.0



79
80
81
82
83
84
85
# File 'build/mongoid-7.3/lib/mongoid/shardable.rb', line 79

def shard_key_selector_in_db
  selector = {}
  shard_key_fields.each do |field|
    selector[field.to_s] = new_record? ? send(field) : attribute_was(field)
  end
  selector
end