Module: Mongoid::Persistable::Unsettable

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Persistable
Defined in:
build/mongoid-7.3/lib/mongoid/persistable/unsettable.rb

Overview

Defines behavior for $unset operations.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#unset(*fields) ⇒ Document

Perform an $unset operation on the provided fields and in the values in the document in memory.

Examples:

Unset the values.

document.unset(:first_name, :last_name, :middle)

Parameters:

  • fields (Array<String, Symbol>)

    The names of the fields to unset.

Returns:

Since:

  • 4.0.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'build/mongoid-7.3/lib/mongoid/persistable/unsettable.rb', line 25

def unset(*fields)
  prepare_atomic_operation do |ops|
    fields.flatten.each do |field|
      normalized = database_field_name(field)
      if executing_atomically?
        process_attribute normalized, nil
      else
        attributes.delete(normalized)
      end
      ops[atomic_attribute_name(normalized)] = true
    end
    { "$unset" => ops }
  end
end