Module: Mongomatic::Modifiers

Included in:
Base
Defined in:
lib/mongomatic/modifiers.rb

Overview

Provides convenience methods for atomic MongoDB operations.

Defined Under Namespace

Classes: UnexpectedFieldType

Instance Method Summary collapse

Instance Method Details

#add_to_set(field, val, update_opts = {}, safe = false) ⇒ Object

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('...'))


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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 = {}) ⇒ Object



226
227
228
# 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) ⇒ Object

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)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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 = {}) ⇒ Object



144
145
146
# 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) ⇒ Object

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

user.pop_first("friend_ids")


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# 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 = {}) ⇒ Object



280
281
282
# 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) ⇒ Object

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

user.pop_last("friend_ids")


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# 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 = {}) ⇒ Object



253
254
255
# 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) ⇒ Object

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")


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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 = {}) ⇒ Object



88
89
90
# 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) ⇒ Object

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"])


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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 = {}) ⇒ Object



117
118
119
# 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) ⇒ Object

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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 = {}) ⇒ Object



30
31
32
# 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) ⇒ Object

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"])


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 = {}) ⇒ Object



60
61
62
# 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) ⇒ Object

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

user.set("name", "Ben")


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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 = {}) ⇒ Object



167
168
169
# 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) ⇒ Object

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

user.unset("name")


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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 = {}) ⇒ Object



189
190
191
# File 'lib/mongomatic/modifiers.rb', line 189

def unset!(field, update_opts={})
  unset(field, update_opts, true)
end