From 2fafddd7fa869ec2d2d53e1242c91f95a2b65a13 Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Fri, 15 May 2009 18:21:10 -0700 Subject: [PATCH] Add Collection#save --- lib/mongo/collection.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 5cbaae2..d54d7f0 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -73,6 +73,15 @@ 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'] + modify({:_id => id}, object) + else + insert(object) + end + end + # Insert +objects+, which are hashes. "<<" is aliased to this method. # Returns either the single inserted object or a new array containing # +objects+. The object(s) may have been modified by the database's PK