Module: Mongoid::Persistence::Atomic

Included in:
Mongoid::Persistence
Defined in:
lib/mongoid/persistence/atomic.rb,
lib/mongoid/persistence/atomic/inc.rb,
lib/mongoid/persistence/atomic/push.rb,
lib/mongoid/persistence/atomic/pull_all.rb,
lib/mongoid/persistence/atomic/operation.rb,
lib/mongoid/persistence/atomic/add_to_set.rb

Overview

:nodoc:

Defined Under Namespace

Classes: AddToSet, Inc, Operation, PullAll, Push

Instance Method Summary collapse

Instance Method Details

#add_to_set(field, value, options = {}) ⇒ Array<Object>

Performs an atomic $addToSet of the provided value on the supplied field. If the field does not exist it will be initialized as an empty array.

If the value already exists on the array it will not be added.

Examples:

Add only a unique value on the field.

person.add_to_set(:aliases, "Bond")

Parameters:

  • field (Symbol)

    The name of the field.

  • value (Object)

    The value to add.

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

    The mongo persistence options.

Returns:

Since:

  • 2.0.0



30
31
32
# File 'lib/mongoid/persistence/atomic.rb', line 30

def add_to_set(field, value, options = {})
  AddToSet.new(self, field, value, options).persist
end

#inc(field, value, options = {}) ⇒ Array<Object>

Performs an atomic $inc of the provided value on the supplied field. If the field does not exist it will be initialized as the provided value.

Examples:

Increment a field.

person.inc(:score, 2)

Parameters:

  • field (Symbol)

    The name of the field.

  • value (Integer)

    The value to increment.

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

    The mongo persistence options.

Returns:

Since:

  • 2.0.0



48
49
50
# File 'lib/mongoid/persistence/atomic.rb', line 48

def inc(field, value, options = {})
  Inc.new(self, field, value, options).persist
end

#pull_all(field, value, options = {}) ⇒ Array<Object>

Performs an atomic $pullAll of the provided value on the supplied field. If the field does not exist it will be initialized as an empty array.

Examples:

Pull the values from the field.

person.pull_all(:aliases, [ "Bond", "James" ])

Parameters:

  • field (Symbol)

    The name of the field.

  • value (Array<Object>)

    The values to pull.

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

    The mongo persistence options.

Returns:

Since:

  • 2.0.0



66
67
68
# File 'lib/mongoid/persistence/atomic.rb', line 66

def pull_all(field, value, options = {})
  PullAll.new(self, field, value, options).persist
end

#push(field, value, options = {}) ⇒ Array<Object>

Performs an atomic $push of the provided value on the supplied field. If the field does not exist it will be initialized as an empty array.

Examples:

Push a value on the field.

person.push(:aliases, "Bond")

Parameters:

  • field (Symbol)

    The name of the field.

  • value (Object)

    The value to push.

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

    The mongo persistence options.

Returns:

Since:

  • 2.0.0



83
84
85
# File 'lib/mongoid/persistence/atomic.rb', line 83

def push(field, value, options = {})
  Push.new(self, field, value, options).persist
end