module Mongomatic::Modifiers

Provides convenience methods for atomic MongoDB operations.

Public Instance Methods

add_to_set(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $addToSet : { field : value } }<br/> Adds value to the array only if its not in the array already.<br/> Or to add many values:<br/> { $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }

user.add_to_set("friend_ids", BSON::ObjectId('...'))
# File lib/mongomatic/modifiers.rb, line 198
def add_to_set(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)

  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  return false if val.nil?
  
  if val.is_a?(Array)
    op  = { "$addToSet" => { mongo_field => { "$each" => val } } }
  else
    op  = { "$addToSet" => { mongo_field => val } }
  end
  
  res = true
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    create_array(val).each do |v|
      hash[field] << v unless hash[field].include?(v)
    end
    true
  end
end
add_to_set!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 226
def add_to_set!(field, val, update_opts={})
  add_to_set(field, val, update_opts, true)
end
inc(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $inc : { field : value } }<br/> Increments field by the number value if field is present in the object, otherwise sets field to the number value.

user.inc("cents_in_wallet", 1000)
# File lib/mongomatic/modifiers.rb, line 124
def inc(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || ["Fixnum","Float"].include?(hash[field].class.to_s)
    raise(UnexpectedFieldType)
  end
  
  op  = { "$inc" => { mongo_field => val } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= 0
    hash[field] += val
    true
  end
end
inc!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 144
def inc!(field, val, update_opts={})
  inc(field, val, update_opts, true)
end
pop_first(field, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $pop : { field : -1 } }<br/> Removes the first element in an array (ADDED in 1.1)

user.pop_first("friend_ids")
# File lib/mongomatic/modifiers.rb, line 260
def pop_first(field, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  op = { "$pop" => { mongo_field => -1 } }
  
  res = true
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    hash[field].shift
    true
  end
end
pop_first!(field, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 280
def pop_first!(field, update_opts={})
  pop_first(field, update_opts, true)
end
pop_last(field, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $pop : { field : 1 } }<br/> Removes the last element in an array (ADDED in 1.1)

user.pop_last("friend_ids")
# File lib/mongomatic/modifiers.rb, line 233
def pop_last(field, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  op = { "$pop" => { mongo_field => 1 } }
  
  res = true
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    hash[field].pop
    true
  end
end
pop_last!(field, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 253
def pop_last!(field, update_opts={})
  pop_last(field, update_opts, true)
end
pull(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $pull : { field : _value } }<br/> Removes all occurrences of value from field, if field is an array. If field is present but is not an array, an error condition is raised.

user.pull("interests", "watching paint dry")
# File lib/mongomatic/modifiers.rb, line 68
def pull(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  op  = { "$pull" => { mongo_field => val } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    hash[field].delete(val)
    true
  end
end
pull!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 88
def pull!(field, val, update_opts={})
  pull(field, val, update_opts, true)
end
pull_all(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $pullAll : { field : value_array } }<br/> Removes all occurrences of each value in value_array from field, if field is an array. If field is present but is not an array, an error condition is raised.

user.pull_all("interests", ["watching paint dry", "sitting on my ass"])
# File lib/mongomatic/modifiers.rb, line 96
def pull_all(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  op  = { "$pullAll" => { mongo_field => create_array(val) } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    create_array(val).each do |v|
      hash[field].delete(v)
    end; true
  end
end
pull_all!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 117
def pull_all!(field, val, update_opts={})
  pull_all(field, val, update_opts, true)
end
push(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $push : { field : value } }<br/> Appends value to field, if field is an existing array, otherwise sets field to the array [value] if field is not present. If field is present but is not an array, error is returned.

# File lib/mongomatic/modifiers.rb, line 10
def push(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)

  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  op  = { "$push" => { mongo_field => val } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    hash[field] << val
    true
  end
end
push!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 30
def push!(field, val, update_opts={})
  push(field, val, update_opts, true)
end
push_all(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $pushAll : { field : value_array } }<br/> Appends each value in value_array to field, if field is an existing array, otherwise sets field to the array value_array if field is not present. If field is present but is not an array, an error condition is raised.

user.push("interests", ["skydiving", "coding"])
# File lib/mongomatic/modifiers.rb, line 39
def push_all(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  unless hash[field].nil? || hash[field].is_a?(Array)
    raise(UnexpectedFieldType)
  end
  
  val = create_array(val)
  op  = { "$pushAll" => { mongo_field => val } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash[field] ||= []
    val.each { |v| hash[field] << v }
    true
  end
end
push_all!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 60
def push_all!(field, val, update_opts={})
  push_all(field, val, update_opts, true)
end
set(field, val, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $set : { field : value } }<br/> Sets field to value. All datatypes are supported with $set.

user.set("name", "Ben")
# File lib/mongomatic/modifiers.rb, line 151
def set(field, val, update_opts={}, safe=false)
  mongo_field = field.to_s
  #field, hash = hash_for_field(field.to_s)
  
  op  = { "$set" => { mongo_field => val } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    set_value_for_key(field.to_s, val)
    #hash[field] = val
    true
  end
end
set!(field, val, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 167
def set!(field, val, update_opts={})
  set(field, val, update_opts, true)
end
unset(field, update_opts={}, safe=false) click to toggle source

MongoDB equivalent: { $unset : { field : 1} }<br/> Deletes a given field. v1.3+

user.unset("name")
# File lib/mongomatic/modifiers.rb, line 174
def unset(field, update_opts={}, safe=false)
  mongo_field = field.to_s
  field, hash = hash_for_field(mongo_field)
  
  op = { "$unset" => { mongo_field => 1 } }
  res = true
  
  safe == true ? res = update!(update_opts, op) : update(update_opts, op)
  
  if res
    hash.delete(field)
    true
  end
end
unset!(field, update_opts={}) click to toggle source
# File lib/mongomatic/modifiers.rb, line 189
def unset!(field, update_opts={})
  unset(field, update_opts, true)
end