minor: update save to use Collection#update instead of the deprecated Collection#repsert

This commit is contained in:
Mike Dirolf 2009-08-14 09:54:52 -04:00
parent 23e09141a0
commit 73233d6589
1 changed files with 11 additions and 5 deletions

View File

@ -97,13 +97,19 @@ module XGen
cursor.next_object # don't need to explicitly close b/c of limit cursor.next_object # don't need to explicitly close b/c of limit
end end
# Save an updated +object+ to the collection, or insert it if it doesn't exist already. # Save a document in this collection
def save(object) #
if id = object[:_id] || object['_id'] # If +to_save+ already has an '_id' then an update (upsert) operation
repsert({:_id => id}, object) # is performed and any existing document with that _id is overwritten.
# Otherwise an insert operation is performed.
#
# :to_save :: the document (a hash) to be saved
def save(to_save)
if id = to_save[:_id] || to_save['_id']
update({:_id => id}, to_save, :upsert => true)
id id
else else
insert(object) insert(to_save)
end end
end end