Sha256: f3ffb25279d13947836f63808fc70adce8d2d88de4e83e07c1c908e2476ad97b

Contents?: true

Size: 1.17 KB

Versions: 23

Compression:

Stored size: 1.17 KB

Contents

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'mongo'
require 'pp'

include Mongo

host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT

puts "Connecting to #{host}:#{port}"
db = Connection.new(host, port).db('ruby-mongo-examples')
coll = db.collection('test')

# Erase all records from collection, if any
coll.remove

# Insert 3 records
3.times { |i| coll.insert({'a' => i+1}) }

# Cursors don't run their queries until you actually attempt to retrieve data
# from them.

# Find returns a Cursor, which is Enumerable. You can iterate:
coll.find().each { |row| pp row }

# You can turn it into an array:
array = coll.find().to_a

# You can iterate after turning it into an array (the cursor will iterate over
# the copy of the array that it saves internally.)
cursor = coll.find()
array = cursor.to_a
cursor.each { |row| pp row }

# You can get the next object
first_object = coll.find().next_document

# next_document returns nil if there are no more objects that match
cursor = coll.find()
obj = cursor.next_document
while obj
  pp obj
  obj = cursor.next_document
end

# Destroy the collection
coll.drop

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
mongo-1.1.1 examples/cursor.rb
mongo-1.1 examples/cursor.rb
mongo-1.0.9 examples/cursor.rb
mongo-1.0.8 examples/cursor.rb
mongo-1.0.7 examples/cursor.rb
mongo-1.0.6 examples/cursor.rb
mongo-1.0.5 examples/cursor.rb
mongo-1.0.4 examples/cursor.rb
mongo-1.0.3 examples/cursor.rb
mongo-1.0.2 examples/cursor.rb
mongo-1.0.1 examples/cursor.rb
mongo-1.0 examples/cursor.rb
mongo-0.20.1 examples/cursor.rb
mongo-0.20 examples/cursor.rb
mongo-0.19.3 examples/cursor.rb
mongo-0.19.2 examples/cursor.rb
mongo-0.19.1 examples/cursor.rb
mongo-0.19 examples/cursor.rb
kbaum-mongo-0.19 examples/cursor.rb
kbaum-mongo-0.18.3.2 examples/cursor.rb