minor: fixed and cleaned up examples

This commit is contained in:
Kyle Banker 2010-01-06 12:21:11 -05:00
parent 44c7145445
commit 941611c139
10 changed files with 35 additions and 27 deletions

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
require 'pp'
@ -8,7 +9,7 @@ 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')
db = Mongo::Connection.new(host, port).db('ruby-mongo-examples')
coll = db.create_collection('test')
# Erase all records from collection, if any
@ -17,7 +18,7 @@ coll.remove
admin = db.admin
# Profiling level set/get
p admin.profiling_level
puts "Profiling level: #{admin.profiling_level}"
# Start profiling everything
admin.profiling_level = :all
@ -31,8 +32,8 @@ admin.profiling_level = :off
# Print all profiling info
pp admin.profiling_info
# Validate returns a hash if all is well or raises an exception if there is a
# problem.
# Validate returns a hash if all is well and
# raises an exception if there is a problem.
info = admin.validate_collection(coll.name)
puts "valid = #{info['ok']}"
puts info['result']

View File

@ -1,4 +1,4 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
include Mongo
@ -10,9 +10,8 @@ puts "Connecting to #{host}:#{port}"
db = Connection.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.
# 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) }

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
require 'pp'
@ -23,7 +24,7 @@ coll.remove
# Find returns a Cursor, which is Enumerable. You can iterate:
coll.find().each { |row| pp row }
# You can turn it into an array
# You can turn it into an array:
array = coll.find().to_a
# You can iterate after turning it into an array (the cursor will iterate over

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
require 'mongo/gridfs'
@ -19,7 +20,7 @@ end
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
# Read it and print out the contents
dump(db, 'foobar') # defined above
dump(db, 'foobar')
# Append more data
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }

View File

@ -1,10 +1,5 @@
class Exception
def errmsg
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
end
end
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
include Mongo
@ -15,6 +10,12 @@ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
puts ">> Connecting to #{host}:#{port}"
db = Connection.new(host, port).db('ruby-mongo-index_test')
class Exception
def errmsg
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
end
end
puts ">> Dropping collection test"
begin
res = db.drop_collection('test')
@ -43,8 +44,8 @@ coll.insert(arr)
puts "inserted"
puts ">> Creating index"
res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1
# res = coll.create_index "all", '_id' => 1, 'number' => 1, 'rndm' => 1, 'msg' => 1
#res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1
res = coll.create_index [[:number, 1], [:rndm, 1], [:msg, 1]]
puts "created index: #{res.inspect}"
# ============================ Mongo Log ============================
# Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index
@ -76,7 +77,7 @@ end
puts ">> Dropping index"
begin
res = coll.drop_index "all_1"
res = coll.drop_index "number_1_rndm_1_msg_1"
puts "dropped : #{res.inspect}"
rescue => e
puts "Error: #{e.errmsg}"

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
include Mongo

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
require 'pp'

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
include Mongo

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
include Mongo

View File

@ -1,4 +1,5 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongo'
require 'pp'