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
-
#add_each_to_set(adds) ⇒ nil
Perform an atomic $addToSet/$each on the matching documents.
-
#add_to_set(adds) ⇒ nil
Execute an atomic $addToSet on the matching documents.
-
#bit(bits) ⇒ nil
Perform an atomic $bit operation on the matching documents.
-
#inc(incs) ⇒ nil
Perform an atomic $inc operation on the matching documents.
-
#mul(factors) ⇒ nil
Perform an atomic $mul operation on the matching documents.
-
#pop(pops) ⇒ nil
Perform an atomic $pop operation on the matching documents.
-
#pull(pulls) ⇒ nil
Perform an atomic $pull operation on the matching documents.
-
#pull_all(pulls) ⇒ nil
Perform an atomic $pullAll operation on the matching documents.
-
#push(pushes) ⇒ nil
Perform an atomic $push operation on the matching documents.
-
#push_all(pushes) ⇒ nil
Perform an atomic $push/$each operation on the matching documents.
-
#rename(renames) ⇒ nil
Perform an atomic $rename of fields on the matching documents.
-
#set(sets) ⇒ nil
Perform an atomic $set of fields on the matching documents.
-
#set_max(fields) ⇒ nil
(also: #clamp_lower_bound)
Performs an atomic $max update operation on the given field or fields.
-
#set_min(fields) ⇒ nil
(also: #clamp_upper_bound)
Performs an atomic $min update operation on the given field or fields.
-
#unset(*unsets) ⇒ nil
Perform an atomic $unset of a field on the matching documents.
Instance Method Details
#add_each_to_set(adds) ⇒ nil
Perform an atomic $addToSet/$each on the matching documents.
34 35 36 |
# File 'lib/mongoid/contextual/atomic.rb', line 34 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.
22 23 24 |
# File 'lib/mongoid/contextual/atomic.rb', line 22 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.
46 47 48 |
# File 'lib/mongoid/contextual/atomic.rb', line 46 def bit(bits) view.update_many("$bit" => collect_operations(bits)) end |
#inc(incs) ⇒ nil
Perform an atomic $inc operation on the matching documents.
58 59 60 |
# File 'lib/mongoid/contextual/atomic.rb', line 58 def inc(incs) view.update_many("$inc" => collect_operations(incs)) end |
#mul(factors) ⇒ nil
Perform an atomic $mul operation on the matching documents.
70 71 72 |
# File 'lib/mongoid/contextual/atomic.rb', line 70 def mul(factors) view.update_many("$mul" => collect_operations(factors)) end |
#pop(pops) ⇒ nil
Perform an atomic $pop operation on the matching documents.
85 86 87 |
# File 'lib/mongoid/contextual/atomic.rb', line 85 def pop(pops) view.update_many("$pop" => collect_operations(pops)) end |
#pull(pulls) ⇒ nil
Expression pulling is not yet supported.
Perform an atomic $pull operation on the matching documents.
99 100 101 |
# File 'lib/mongoid/contextual/atomic.rb', line 99 def pull(pulls) view.update_many("$pull" => collect_operations(pulls)) end |
#pull_all(pulls) ⇒ nil
Perform an atomic $pullAll operation on the matching documents.
111 112 113 |
# File 'lib/mongoid/contextual/atomic.rb', line 111 def pull_all(pulls) view.update_many("$pullAll" => collect_operations(pulls)) end |
#push(pushes) ⇒ nil
Perform an atomic $push operation on the matching documents.
123 124 125 |
# File 'lib/mongoid/contextual/atomic.rb', line 123 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.
135 136 137 |
# File 'lib/mongoid/contextual/atomic.rb', line 135 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.
147 148 149 150 151 152 153 |
# File 'lib/mongoid/contextual/atomic.rb', line 147 def rename(renames) operations = renames.inject({}) do |ops, (old_name, new_name)| ops[old_name] = new_name.to_s ops end view.update_many("$rename" => collect_operations(operations)) end |
#set(sets) ⇒ nil
Perform an atomic $set of fields on the matching documents.
163 164 165 |
# File 'lib/mongoid/contextual/atomic.rb', line 163 def set(sets) view.update_many("$set" => collect_operations(sets)) end |
#set_max(fields) ⇒ nil Also known as: clamp_lower_bound
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.
223 224 225 |
# File 'lib/mongoid/contextual/atomic.rb', line 223 def set_max(fields) view.update_many("$max" => collect_operations(fields)) end |
#set_min(fields) ⇒ nil Also known as: clamp_upper_bound
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.
200 201 202 |
# File 'lib/mongoid/contextual/atomic.rb', line 200 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.
178 179 180 |
# File 'lib/mongoid/contextual/atomic.rb', line 178 def unset(*unsets) view.update_many('$unset' => collect_unset_operations(unsets)) end |