mongo-ruby-driver/examples/types.rb

36 lines
1.0 KiB
Ruby
Raw Normal View History

2009-02-03 17:15:35 +00:00
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
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'] || Mongo::Mongo::DEFAULT_PORT
2009-02-03 17:15:35 +00:00
puts "Connecting to #{host}:#{port}"
db = Mongo::Mongo.new(host, port).db('ruby-mongo-examples')
2009-02-03 17:15:35 +00:00
coll = db.collection('test')
# Remove all records, if any
coll.clear
# 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-02-03 17:43:22 +00:00
pp coll.find().next_object
2009-02-03 17:15:35 +00:00
coll.clear