Created
October 1, 2009 15:01
-
-
Save audionerd/199032 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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