mongo-ruby-driver/docs/examples/simple.rb

26 lines
633 B
Ruby
Raw Normal View History

2010-01-06 17:21:11 +00:00
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
2008-11-22 01:00:51 +00:00
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