Module: Mongoid::Contextual::Atomic

Defined in:
lib/mongoid/contextual/atomic.rb

Instance Method Summary collapse

Instance Method Details

#add_to_set(field, value) ⇒ nil

Execute an atomic $addToSet on the matching documents.

Examples:

Add the value to the set.

context.add_to_set(:members, "Dave")

Parameters:

  • field (String, Symbol)

    The name of the field to add to.

  • value (Object)

    The single value to add.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



17
18
19
# File 'lib/mongoid/contextual/atomic.rb', line 17

def add_to_set(field, value)
  query.update_all("$addToSet" => { field => value })
end

#bit(field, value) ⇒ nil

Perform an atomic $bit operation on the matching documents.

Examples:

Perform the bitwise op.

context.bit(:likes, { and: 14, or: 4 })

Parameters:

  • field (String, Symbol)

    The name of the field to operate on.

  • value (Hash)

    The bitwise operations to perform. Keys may be “and” or “or” and must have numeric values.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



33
34
35
# File 'lib/mongoid/contextual/atomic.rb', line 33

def bit(field, value)
  query.update_all("$bit" => { field => value })
end

#inc(field, value) ⇒ nil

Perform an atomic $inc operation on the matching documents.

Examples:

Perform the atomic increment.

context.inc(:likes, 10)

Parameters:

  • field (String, Symbol)

    The field to increment.

  • value (Integer)

    The amount to increment by.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



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

def inc(field, value)
  query.update_all("$inc" => { field => value })
end

#pop(field, value) ⇒ nil

Perform an atomic $pop operation on the matching documents.

Examples:

Pop the first value on the matches.

context.pop(:members, -1)

Pop the last value on the matches.

context.pop(:members, 1)

Parameters:

  • field (String, Symbol)

    The name of the array field to pop from.

  • value (Integer)

    1 to pop from the end, -1 to pop from the front.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



68
69
70
# File 'lib/mongoid/contextual/atomic.rb', line 68

def pop(field, value)
  query.update_all("$pop" => { field => value })
end

#pull(field, value) ⇒ nil

Note:

Expression pulling is not yet supported.

Perform an atomic $pull operation on the matching documents.

Examples:

Pull the value from the matches.

context.pull(:members, "Dave")

Parameters:

  • field (String, Symbol)

    The field to pull from.

  • value (Object)

    The single value to pull.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



85
86
87
# File 'lib/mongoid/contextual/atomic.rb', line 85

def pull(field, value)
  query.update_all("$pull" => { field => value })
end

#pull_all(field, values) ⇒ nil

Perform an atomic $pullAll operation on the matching documents.

Examples:

Pull all the matching values from the matches.

context.pull_all(:members, [ "Alan", "Vince" ])

Parameters:

  • field (String, Symbol)

    The field to pull from.

  • values (Array<Object>)

    The values to pull.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



100
101
102
# File 'lib/mongoid/contextual/atomic.rb', line 100

def pull_all(field, values)
  query.update_all("$pullAll" => { field => values })
end

#push(field, value) ⇒ nil

Perform an atomic $push operation on the matching documents.

Examples:

Push the value to the matching docs.

context.push(:members, "Alan")

Parameters:

  • field (String, Symbol)

    The field to push to.

  • value (Object)

    The value to push.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



115
116
117
# File 'lib/mongoid/contextual/atomic.rb', line 115

def push(field, value)
  query.update_all("$push" => { field => value })
end

#push_all(field, values) ⇒ nil

Perform an atomic $pushAll operation on the matching documents.

Examples:

Push the values to the matching docs.

context.push(:members, [ "Alan", "Fletch" ])

Parameters:

  • field (String, Symbol)

    The field to push to.

  • values (Array<Object>)

    The values to push.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



130
131
132
# File 'lib/mongoid/contextual/atomic.rb', line 130

def push_all(field, values)
  query.update_all("$pushAll" => { field => values })
end

#rename(old_name, new_name) ⇒ nil

Perform an atomic $rename of fields on the matching documents.

Examples:

Rename the fields on the matching documents.

context.rename(:members, :artists)

Parameters:

  • old_name (String, Symbol)

    The old field name.

  • new_name (String, Symbol)

    The new field name.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



145
146
147
# File 'lib/mongoid/contextual/atomic.rb', line 145

def rename(old_name, new_name)
  query.update_all("$rename" => { old_name.to_s => new_name.to_s })
end

#set(field, value) ⇒ nil

Perform an atomic $set of fields on the matching documents.

Examples:

Set the field value on the matches.

context.set(:name, "Depeche Mode")

Parameters:

  • field (String, Symbol)

    The name of the field.

  • value (Object)

    The value to set.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



160
161
162
# File 'lib/mongoid/contextual/atomic.rb', line 160

def set(field, value)
  query.update_all("$set" => { field => value })
end

#unset(field) ⇒ nil

Perform an atomic $unset of a field on the matching documents.

Examples:

Unset the field on the matches.

context.unset(:name)

Parameters:

  • field (String, Symbol)

    The name of the field.

Returns:

  • (nil)

    Nil.

Since:

  • 3.0.0



174
175
176
# File 'lib/mongoid/contextual/atomic.rb', line 174

def unset(field)
  query.update_all("$unset" => { field => true })
end