mongo-ruby-driver/examples/simple.rb

24 lines
619 B
Ruby
Raw Normal View History

2008-11-22 01:00:51 +00:00
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
include Mongo
2008-11-22 01:00:51 +00:00
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')
2008-11-22 01:00:51 +00:00
coll = db.collection('test')
2009-02-03 17:15:35 +00:00
# Erase all records from collection, if any
2010-01-05 23:14:48 +00:00
coll.remove
2008-11-22 01:00:51 +00:00
2009-02-03 17:15:35 +00:00
# Insert 3 records
2008-12-17 16:43:00 +00:00
3.times { |i| coll.insert({'a' => i+1}) }
puts "There are #{coll.count()} records in the test collection. Here they are:"
2008-11-22 01:00:51 +00:00
coll.find().each { |doc| puts doc.inspect }
2009-02-03 17:15:35 +00:00
# Destroy the collection
coll.drop