File: legacy.rb

package info (click to toggle)
ruby-em-mongo 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ruby: 2,728; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 959 bytes parent folder | download | duplicates (3)
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
#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')
  db = conn.db('my_database')
  collection = db.collection('my_collection')
  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