1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#bundle exec ruby examples/readme.rb
require 'em-mongo'
require 'eventmachine'
EM.run do
db = EM::Mongo::Connection.new('localhost').db('my_database')
collection = db.collection('my_collection')
EM.next_tick do
(1..10).each do |i|
collection.insert( { :revolution => i } )
end
#find returns an EM::Mongo::Cursor
cursor = collection.find
#most cursor methods return an EM::Mongo::RequestResponse,
#which is an EventMachine::Deferrable
resp = cursor.to_a
#when em-mongo IO methods succeed, they
#will always call back with the return
#value you would have expected from the
#synchronous version of the method from
#the mongo-ruby-driver
resp.callback do |documents|
puts "I just got #{documents.length} documents! I'm really cool!"
end
#when em-mongo IO methods fail, they
#errback with an array in the form
#[ErrorClass, "error message"]
resp.errback do |err|
raise *err
end
#iterate though each result in a query
collection.find( :revolution => { "$gt" => 5 } ).limit(1).skip(1).each do |doc|
#unlike the mongo-ruby-driver, each returns null at the end of the cursor
if doc
puts "Revolution ##{doc['revolution']}"
end
end
#add an index
collection.create_index [[:revolution, -1]]
#insert a document and ensure it gets written
save_resp = collection.safe_save( { :hi => "there" }, :last_error_params => {:fsync=>true} )
save_resp.callback { puts "Hi is there, let us give thanks" }
save_resp.errback { |err| puts "AAAAAAAAAAAAAAAARGH! Oh why! WHY!?!?!" }
collection.drop
EM.add_periodic_timer(1) { EM.stop }
end
end
|