From 73233d65890a310050c633ba40bd3bef2dd0f4f3 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Fri, 14 Aug 2009 09:54:52 -0400 Subject: [PATCH] minor: update save to use Collection#update instead of the deprecated Collection#repsert --- lib/mongo/collection.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index cedce42..6cda9ca 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -97,13 +97,19 @@ module XGen cursor.next_object # don't need to explicitly close b/c of limit end - # Save an updated +object+ to the collection, or insert it if it doesn't exist already. - def save(object) - if id = object[:_id] || object['_id'] - repsert({:_id => id}, object) + # Save a document in this collection + # + # If +to_save+ already has an '_id' then an update (upsert) operation + # 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 else - insert(object) + insert(to_save) end end