Module: Mongoid::Contextual::Atomic

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

Overview

Mixin module included in Mongoid::Criteria which provides a direct method interface to MongoDB's Update Operators ($set, $pull, $inc, etc.) These operators can be applied to update all documents in the database within the criteria scope, without loading each document into Mongoid's memory.

Instance Method Summary collapse

Instance Method Details

#add_each_to_set(adds) ⇒ nil

Perform an atomic $addToSet/$each on the matching documents.

Examples:

Add the value to the set.

context.add_each_to_set(members: ["Dave", "Bill"], genres: ["Electro", "Disco"])

Parameters:

  • adds (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



31
32
33
# File 'lib/mongoid/contextual/atomic.rb', line 31

def add_each_to_set(adds)
  view.update_many('$addToSet' => collect_each_operations(adds))
end

#add_to_set(adds) ⇒ nil

Execute an atomic $addToSet on the matching documents.

Examples:

Add the value to the set.

context.add_to_set(members: "Dave", genres: "Electro")

Parameters:

  • adds (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



19
20
21
# File 'lib/mongoid/contextual/atomic.rb', line 19

def add_to_set(adds)
  view.update_many('$addToSet' => collect_operations(adds))
end

#bit(bits) ⇒ nil

Perform an atomic $bit operation on the matching documents.

Examples:

Perform the bitwise op.

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

Parameters:

  • bits (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



43
44
45
# File 'lib/mongoid/contextual/atomic.rb', line 43

def bit(bits)
  view.update_many('$bit' => collect_operations(bits))
end

#inc(incs) ⇒ nil

Perform an atomic $inc operation on the matching documents.

Examples:

Perform the atomic increment.

context.inc(likes: 10)

Parameters:

  • incs (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



55
56
57
# File 'lib/mongoid/contextual/atomic.rb', line 55

def inc(incs)
  view.update_many('$inc' => collect_operations(incs))
end

#mul(factors) ⇒ nil

Perform an atomic $mul operation on the matching documents.

Examples:

Perform the atomic multiplication.

context.mul(likes: 10)

Parameters:

  • factors (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



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

def mul(factors)
  view.update_many('$mul' => collect_operations(factors))
end

#pop(pops) ⇒ 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:

  • pops (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



82
83
84
# File 'lib/mongoid/contextual/atomic.rb', line 82

def pop(pops)
  view.update_many('$pop' => collect_operations(pops))
end

#pull(pulls) ⇒ 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:

  • pulls (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



96
97
98
# File 'lib/mongoid/contextual/atomic.rb', line 96

def pull(pulls)
  view.update_many('$pull' => collect_operations(pulls))
end

#pull_all(pulls) ⇒ 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:

  • pulls (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



108
109
110
# File 'lib/mongoid/contextual/atomic.rb', line 108

def pull_all(pulls)
  view.update_many('$pullAll' => collect_operations(pulls))
end

#push(pushes) ⇒ nil

Perform an atomic $push operation on the matching documents.

Examples:

Push the value to the matching docs.

context.push(members: "Alan")

Parameters:

  • pushes (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



120
121
122
# File 'lib/mongoid/contextual/atomic.rb', line 120

def push(pushes)
  view.update_many('$push' => collect_operations(pushes))
end

#push_all(pushes) ⇒ nil

Perform an atomic $push/$each operation on the matching documents.

Examples:

Push the values to the matching docs.

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

Parameters:

  • pushes (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



132
133
134
# File 'lib/mongoid/contextual/atomic.rb', line 132

def push_all(pushes)
  view.update_many('$push' => collect_each_operations(pushes))
end

#rename(renames) ⇒ nil

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

Examples:

Rename the fields on the matching documents.

context.rename(members: :artists)

Parameters:

  • renames (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



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

def rename(renames)
  operations = renames.each_with_object({}) do |(old_name, new_name), ops|
    ops[old_name] = new_name.to_s
  end
  view.update_many('$rename' => collect_operations(operations))
end

#set(sets) ⇒ 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:

  • sets (Hash)

    The operations.

Returns:

  • (nil)

    Nil.



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

def set(sets)
  view.update_many('$set' => collect_operations(sets))
end

#set_max(fields) ⇒ nil Also known as: clamp_lower_bound

Note:

Because of the existence of Mongoid::Contextual::Aggregable::Mongo#max, this method cannot be named #max, and thus breaks that convention of other similar methods of being named for the MongoDB operation they perform.

Performs an atomic $max update operation on the given field or fields. Each field will be set to the maximum of [current_value, given value]. This has the effect of making sure that each field is no smaller than the given value; in other words, the given value is the effective minimum for that field.

Examples:

Set "views" to be no less than 100.

context.set_max(views: 100)

Parameters:

  • fields (Hash)

    The fields with the minimum value that each may be set to.

Returns:

  • (nil)

    Nil.



219
220
221
# File 'lib/mongoid/contextual/atomic.rb', line 219

def set_max(fields)
  view.update_many('$max' => collect_operations(fields))
end

#set_min(fields) ⇒ nil Also known as: clamp_upper_bound

Note:

Because of the existence of Mongoid::Contextual::Aggregable::Mongo#min, this method cannot be named #min, and thus breaks that convention of other similar methods of being named for the MongoDB operation they perform.

Performs an atomic $min update operation on the given field or fields. Each field will be set to the minimum of [current_value, given value]. This has the effect of making sure that each field is no larger than the given value; in other words, the given value is the effective maximum for that field.

Examples:

Set "views" to be no more than 100.

context.set_min(views: 100)

Parameters:

  • fields (Hash)

    The fields with the maximum value that each may be set to.

Returns:

  • (nil)

    Nil.



196
197
198
# File 'lib/mongoid/contextual/atomic.rb', line 196

def set_min(fields)
  view.update_many('$min' => collect_operations(fields))
end

#unset(*unsets) ⇒ nil

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

Examples:

Unset the field on the matches.

context.unset(:name)

Parameters:

  • *unsets ([ String | Symbol | Array<String | Symbol> | Hash ]...)

    The name(s) of the field(s) to unset. If a Hash is specified, its keys will be used irrespective of value, even if the value is nil or false.

Returns:

  • (nil)

    Nil.



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

def unset(*unsets)
  view.update_many('$unset' => collect_unset_operations(unsets))
end