Module: Mongoid::Persistable::Pullable

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

Overview

Defines behavior for $pull and $pullAll operations.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#pull(pulls) ⇒ Document

Note:

If duplicate values are found they will all be pulled.

Pull single values from the provided arrays.

Examples:

Pull a value from the array.

document.pull(names: "Jeff", levels: 5)

Parameters:

  • pulls (Hash)

    The field/value pull pairs.

Returns:

Since:

  • 4.0.0



25
26
27
28
29
30
31
32
33
# File 'build/mongoid-7.3/lib/mongoid/persistable/pullable.rb', line 25

def pull(pulls)
  prepare_atomic_operation do |ops|
    process_atomic_operations(pulls) do |field, value|
      (send(field) || []).delete(value)
      ops[atomic_attribute_name(field)] = value
    end
    { "$pull" => ops }
  end
end

#pull_all(pulls) ⇒ Document

Pull multiple values from the provided array fields.

Examples:

Pull values from the arrays.

document.pull_all(names: [ "Jeff", "Bob" ], levels: [ 5, 6 ])

Parameters:

  • pulls (Hash)

    The pull all operations.

Returns:

Since:

  • 4.0.0



45
46
47
48
49
50
51
52
53
54
# File 'build/mongoid-7.3/lib/mongoid/persistable/pullable.rb', line 45

def pull_all(pulls)
  prepare_atomic_operation do |ops|
    process_atomic_operations(pulls) do |field, value|
      existing = send(field) || []
      value.each{ |val| existing.delete(val) }
      ops[atomic_attribute_name(field)] = value
    end
    { "$pullAll" => ops }
  end
end