mongo-ruby-driver/examples/types.rb

37 lines
1.0 KiB
Ruby
Raw Normal View History

2010-01-06 17:21:11 +00:00
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2009-02-03 17:15:35 +00:00
require 'mongo'
require 'pp'
include Mongo
2009-02-03 17:15:35 +00:00
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
2009-02-03 17:15:35 +00:00
puts "Connecting to #{host}:#{port}"
db = Connection.new(host, port).db('ruby-mongo-examples')
2009-02-03 17:15:35 +00:00
coll = db.collection('test')
# Remove all records, if any
2010-01-05 23:14:48 +00:00
coll.remove
2009-02-03 17:15:35 +00:00
# Insert record with all sorts of values
coll.insert('array' => [1, 2, 3],
'string' => 'hello',
'hash' => {'a' => 1, 'b' => 2},
2009-02-03 19:19:12 +00:00
'date' => Time.now, # milliseconds only; microseconds are not stored
2009-02-03 17:15:35 +00:00
'oid' => ObjectID.new,
'binary' => Binary.new([1, 2, 3]),
'int' => 42,
'float' => 33.33333,
'regex' => /foobar/i,
'boolean' => true,
'where' => Code.new('this.x == 3'),
2009-03-12 19:33:13 +00:00
'dbref' => DBRef.new(coll.name, ObjectID.new),
2009-02-03 17:15:35 +00:00
'null' => nil,
'symbol' => :zildjian)
2009-12-16 19:03:15 +00:00
pp coll.find().next_document
2009-02-03 17:15:35 +00:00
2010-01-05 23:14:48 +00:00
coll.remove