/
legacy.rb
45 lines (30 loc) · 1.15 KB
/
legacy.rb
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
#bundle exec ruby examples/legacy.rb
require 'em-mongo'
require 'em-mongo/prev.rb'
require 'eventmachine'
EM.run do
conn = EM::Mongo::Connection.new('localhost',27017)
db = conn.db('my_database')
collection = db.collection('my_collection')
resp = conn.db('admin').authenticate('test','test')
resp.callback{|response| puts "successfully authenticated #{response}"}
resp.errback {|response| puts "error on authentication #{response}"; return}
EM.next_tick do
(1..10).each do |i|
conn.insert('my_database.my_collection', { :revolution => i } )
end
conn.update('my_database.my_collection', {:revolution => 9}, {:revolution => 8.5})
conn.delete('my_database.my_collection', {:revolution => 1})
collection.find do |documents|
puts "I just got #{documents.length} documents! I'm really cool!"
end
#iterate though each result in a query
collection.find( {:revolution => { "$gt" => 5 }}, :limit =>1, :skip => 1, :order => [:revolution, -1]) do |docs|
docs.each do |doc|
puts "Revolution ##{doc['revolution']}"
end
end
collection.drop
EM.add_periodic_timer(1) { EM.stop }
end
end