Skip to content

Instantly share code, notes, and snippets.

@audionerd
Created October 1, 2009 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audionerd/199032 to your computer and use it in GitHub Desktop.
Save audionerd/199032 to your computer and use it in GitHub Desktop.
require 'mongo'
include Mongo
FIRST = 1
SECOND = 2
THIRD = 3
FOURTH = 4
db = Connection.new('localhost').db('test_safe_updates')
coll = db.collection('items')
coll.clear
# create a new object
id = coll.insert({ :state => FIRST })
# update from FIRST to SECOND
from = { '_id' => id, :state => FIRST }
to = { '$set' => { :state => SECOND } }
result = coll.update(from, to, :safe => true)
puts coll.find().each { |doc| puts doc.inspect }
# now try from THIRD (which doesn't exist) to FOURTH
from = { '_id' => id, :state => THIRD }
to = { '$set' => { :state => FOURTH } }
# shouldn't this give me an error? or return something other than nil?
result = coll.update(from, to, :safe => true)
puts db.error
puts coll.find().each { |doc| puts doc.inspect }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment