diff --git a/examples/admin.rb b/examples/admin.rb new file mode 100644 index 0000000..5d36a91 --- /dev/null +++ b/examples/admin.rb @@ -0,0 +1,35 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' +require 'pp' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') +coll = db.collection('test') + +# Erase all records from collection, if any +coll.clear + +admin = db.admin + +# Profiling level set/get +p admin.profiling_level + +# Start profiling everything +admin.profiling_level = :all + +# Read records, creating a profiling event +coll.find().to_a + +# Stop profiling +admin.profiling_level = :off + +# Print all profiling info +pp admin.profiling_info + +# Destroy the collection +coll.drop diff --git a/examples/benchmarks.rb b/examples/benchmarks.rb index deaf4b5..c708c16 100644 --- a/examples/benchmarks.rb +++ b/examples/benchmarks.rb @@ -8,7 +8,7 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT puts "Connecting to #{host}:#{port}" -db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-examples-complex') +db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-examples') coll = db.collection('test') coll.clear diff --git a/examples/capped.rb b/examples/capped.rb new file mode 100644 index 0000000..5310ad8 --- /dev/null +++ b/examples/capped.rb @@ -0,0 +1,23 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') +db.drop_collection('test') + +# A capped collection has a max size and optionally a max number of records. +# Old records get pushed out by new ones once the size or max num records is +# reached. +coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12) + +100.times { |i| coll.insert('a' => i+1) } + +# We will only see the last 12 records +coll.find().each { |row| p row } + +coll.drop diff --git a/examples/index_test.rb b/examples/index_test.rb index 21230b9..c38a569 100644 --- a/examples/index_test.rb +++ b/examples/index_test.rb @@ -1,6 +1,3 @@ -require "rubygems" -require "benchwarmer" - class Exception def errmsg "%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"] @@ -38,7 +35,7 @@ OBJS_COUNT = 100 puts ">> Generating test data" msgs = %w{hola hello aloha ciao} -arr = OBJS_COUNT.times.map {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }} +arr = (0...OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }} puts "generated" puts ">> Inserting data (#{arr.size})" diff --git a/examples/info.rb b/examples/info.rb new file mode 100644 index 0000000..a727bfb --- /dev/null +++ b/examples/info.rb @@ -0,0 +1,30 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') +coll = db.collection('test') + +# Erase all records from collection, if any +coll.clear + +# Insert 3 records +3.times { |i| coll.insert({'a' => i+1}) } + +# Collection names in database +p db.collection_names + +# More information about each collection +p db.collections_info + +# Index information +db.create_index('test', 'index_name', ['a']) +p db.index_information('test') + +# Destroy the collection +coll.drop diff --git a/examples/queries.rb b/examples/queries.rb new file mode 100644 index 0000000..ecdfcfe --- /dev/null +++ b/examples/queries.rb @@ -0,0 +1,69 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' +require 'pp' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') +coll = db.collection('test') + +# Remove all records, if any +coll.clear + +# Insert three records +coll.insert('a' => 1) +coll.insert('a' => 2) +coll.insert('b' => 3) + +# Count. +puts "There are #{coll.count()} records." + +# Find all records. find() returns a Cursor. +cursor = coll.find() + +# Print them. Note that all records have an _id automatically added by the +# database. See pk.rb for an example of how to use a primary key factory to +# generate your own values for _id. +cursor.each { |row| pp row } + +# Cursor has a to_a method that slurps all records into memory. +rows = coll.find().to_a +rows.each { |row| pp row } + +# See Collection#find. From now on in this file, we won't be printing the +# records we find. +coll.find('a' => 1) + +# Find records sort by 'a', offset 1, limit 2 records. +# Sort can be single name, array, or hash. +coll.find({}, {:offset => 1, :limit => 2, :sort => 'a'}) + +# Find all records with 'a' > 1. There is also $lt, $gte, and $lte. +coll.find({'a' => {'$gt' => 1}}) +coll.find({'a' => {'$gt' => 1, '$lte' => 3}}) + +# Find all records with 'a' in a set of values. +coll.find('a' => {'$in' => [1,2]}) + +# Find by regexp +coll.find('a' => /[1|2]/) + +# Print query explanation +pp coll.find('a' => /[1|2]/).explain() + +# Use a hint with a query. Need an index. Hints can be stored with the +# collection, in which case they will be used with all queries, or they can be +# specified per query, in which case that hint overrides the hint associated +# with the collection if any. +coll.create_index('test_a_index', 'a') +coll.hint = 'a' + +# You will see a different explanation now that the hint is in place +pp coll.find('a' => /[1|2]/).explain() + +# Override hint for single query +coll.find({'a' => 1}, :hint => 'b') diff --git a/examples/simple.rb b/examples/simple.rb index 5a46c2d..3415fe3 100644 --- a/examples/simple.rb +++ b/examples/simple.rb @@ -7,11 +7,17 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT puts "Connecting to #{host}:#{port}" -db = Mongo.new(host, port).db('ruby-mongo-examples-simple') +db = Mongo.new(host, port).db('ruby-mongo-examples') coll = db.collection('test') + +# Erase all records from collection, if any coll.clear +# Insert 3 records 3.times { |i| coll.insert({'a' => i+1}) } puts "There are #{coll.count()} records in the test collection. Here they are:" coll.find().each { |doc| puts doc.inspect } + +# Destroy the collection +coll.drop diff --git a/examples/strict.rb b/examples/strict.rb new file mode 100644 index 0000000..b2f99a4 --- /dev/null +++ b/examples/strict.rb @@ -0,0 +1,34 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') + +db.drop_collection('does-not-exist') +db.create_collection('test') + +db.strict = true + +begin + # Can't reference collection that does not exist + db.collection('does-not-exist') + puts "error: expected exception" +rescue => ex + puts "expected exception: #{ex}" +end + +begin + # Can't create collection that already exists + db.create_collection('test') + puts "error: expected exception" +rescue => ex + puts "expected exception: #{ex}" +end + +db.strict = false +db.drop_collection('test') diff --git a/examples/types.rb b/examples/types.rb new file mode 100644 index 0000000..f04d3df --- /dev/null +++ b/examples/types.rb @@ -0,0 +1,40 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' +require 'pp' + +include XGen::Mongo::Driver + +host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' +port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + +puts "Connecting to #{host}:#{port}" +db = Mongo.new(host, port).db('ruby-mongo-examples') +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}, + 'date' => Time.now, # seconds only; millisecs are not stored + 'oid' => ObjectID.new, + 'binary' => Binary.new([1, 2, 3]), + 'int' => 42, + 'float' => 33.33333, + 'regex' => /foobar/i, + 'boolean' => true, + '$where' => 'this.x == 3', # special case of string + 'dbref' => DBRef.new(nil, 'dbref', db, coll.name, ObjectID.new), + +# NOTE: the undefined type is not saved to the database properly. This is a +# Mongo bug. However, the undefined type may go away completely. +# 'undef' => Undefined.new, + + 'null' => nil, + 'symbol' => :zildjian) + +coll.find().each { |row| pp row } + +coll.clear