module Mongoid::Contextual::Atomic
Public Instance Methods
Execute an atomic $addToSet on the matching documents.
@example Add the value to the set.
context.add_to_set(members: "Dave", genres: "Electro")
@param [ Hash ] adds The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 16 def add_to_set(adds) query.update_all("$addToSet" => collect_operations(adds)) end
Perform an atomic $bit operation on the matching documents.
@example Perform the bitwise op.
context.bit(likes: { and: 14, or: 4 })
@param [ Hash ] bits The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 30 def bit(bits) query.update_all("$bit" => collect_operations(bits)) end
Perform an atomic $inc operation on the matching documents.
@example Perform the atomic increment.
context.inc(likes: 10)
@param [ Hash ] incs The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 44 def inc(incs) query.update_all("$inc" => collect_operations(incs)) end
Perform an atomic $pop operation on the matching documents.
@example Pop the first value on the matches.
context.pop(members: -1)
@example Pop the last value on the matches.
context.pop(members: 1)
@param [ Hash ] pops The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 61 def pop(pops) query.update_all("$pop" => collect_operations(pops)) end
Perform an atomic $pull operation on the matching documents.
@example Pull the value from the matches.
context.pull(members: "Dave")
@note Expression pulling is not yet supported.
@param [ Hash ] pulls The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 77 def pull(pulls) query.update_all("$pull" => collect_operations(pulls)) end
Perform an atomic $pullAll operation on the matching documents.
@example Pull all the matching values from the matches.
context.pull_all(:members, [ "Alan", "Vince" ])
@param [ Hash ] pulls The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 91 def pull_all(pulls) query.update_all("$pullAll" => collect_operations(pulls)) end
Perform an atomic $push operation on the matching documents.
@example Push the value to the matching docs.
context.push(members: "Alan")
@param [ Hash ] pushes The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 105 def push(pushes) query.update_all("$push" => collect_operations(pushes)) end
Perform an atomic $pushAll operation on the matching documents.
@example Push the values to the matching docs.
context.push(members: [ "Alan", "Fletch" ])
@param [ Hash ] pushes The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 119 def push_all(pushes) query.update_all("$pushAll" => collect_operations(pushes)) end
Perform an atomic $rename of fields on the matching documents.
@example Rename the fields on the matching documents.
context.rename(members: :artists)
@param [ Hash ] renames The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 133 def rename(renames) operations = renames.inject({}) do |ops, (old_name, new_name)| ops[old_name] = new_name.to_s ops end query.update_all("$rename" => collect_operations(operations)) end
Perform an atomic $set of fields on the matching documents.
@example Set the field value on the matches.
context.set(name: "Depeche Mode")
@param [ Hash ] sets The operations.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 151 def set(sets) query.update_all("$set" => collect_operations(sets)) end
Perform an atomic $unset of a field on the matching documents.
@example Unset the field on the matches.
context.unset(:name)
@param [ String, Symbol
, Array ] fields The name of the fields.
@return [ nil ] Nil.
@since 3.0.0
# File lib/mongoid/contextual/atomic.rb, line 165 def unset(*args) fields = args.__find_args__.collect { |f| [database_field_name(f), true] } query.update_all("$unset" => Hash[fields]) end
Private Instance Methods
# File lib/mongoid/contextual/atomic.rb, line 172 def collect_operations(ops) ops.inject({}) do |operations, (field, value)| operations[database_field_name(field)] = value.mongoize operations end end