minor: fixed and cleaned up examples
This commit is contained in:
parent
44c7145445
commit
941611c139
|
@ -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'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
|
||||||
|
@ -8,7 +9,7 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
||||||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
||||||
|
|
||||||
puts "Connecting to #{host}:#{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')
|
coll = db.create_collection('test')
|
||||||
|
|
||||||
# Erase all records from collection, if any
|
# Erase all records from collection, if any
|
||||||
|
@ -17,7 +18,7 @@ coll.remove
|
||||||
admin = db.admin
|
admin = db.admin
|
||||||
|
|
||||||
# Profiling level set/get
|
# Profiling level set/get
|
||||||
p admin.profiling_level
|
puts "Profiling level: #{admin.profiling_level}"
|
||||||
|
|
||||||
# Start profiling everything
|
# Start profiling everything
|
||||||
admin.profiling_level = :all
|
admin.profiling_level = :all
|
||||||
|
@ -31,8 +32,8 @@ admin.profiling_level = :off
|
||||||
# Print all profiling info
|
# Print all profiling info
|
||||||
pp admin.profiling_info
|
pp admin.profiling_info
|
||||||
|
|
||||||
# Validate returns a hash if all is well or raises an exception if there is a
|
# Validate returns a hash if all is well and
|
||||||
# problem.
|
# raises an exception if there is a problem.
|
||||||
info = admin.validate_collection(coll.name)
|
info = admin.validate_collection(coll.name)
|
||||||
puts "valid = #{info['ok']}"
|
puts "valid = #{info['ok']}"
|
||||||
puts info['result']
|
puts info['result']
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||||
require 'mongo'
|
require 'mongo'
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
@ -10,9 +10,8 @@ puts "Connecting to #{host}:#{port}"
|
||||||
db = Connection.new(host, port).db('ruby-mongo-examples')
|
db = Connection.new(host, port).db('ruby-mongo-examples')
|
||||||
db.drop_collection('test')
|
db.drop_collection('test')
|
||||||
|
|
||||||
# A capped collection has a max size and optionally a max number of records.
|
# 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
|
# Old records get pushed out by new ones once the size or max num records is reached.
|
||||||
# reached.
|
|
||||||
coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
|
coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
|
||||||
|
|
||||||
100.times { |i| coll.insert('a' => i+1) }
|
100.times { |i| coll.insert('a' => i+1) }
|
||||||
|
|
|
@ -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'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ coll.remove
|
||||||
# Find returns a Cursor, which is Enumerable. You can iterate:
|
# Find returns a Cursor, which is Enumerable. You can iterate:
|
||||||
coll.find().each { |row| pp row }
|
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
|
array = coll.find().to_a
|
||||||
|
|
||||||
# You can iterate after turning it into an array (the cursor will iterate over
|
# You can iterate after turning it into an array (the cursor will iterate over
|
||||||
|
|
|
@ -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'
|
||||||
require 'mongo/gridfs'
|
require 'mongo/gridfs'
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ end
|
||||||
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
||||||
|
|
||||||
# Read it and print out the contents
|
# Read it and print out the contents
|
||||||
dump(db, 'foobar') # defined above
|
dump(db, 'foobar')
|
||||||
|
|
||||||
# Append more data
|
# Append more data
|
||||||
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
|
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
class Exception
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||||
def errmsg
|
|
||||||
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
||||||
require 'mongo'
|
require 'mongo'
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
@ -15,6 +10,12 @@ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
||||||
puts ">> Connecting to #{host}:#{port}"
|
puts ">> Connecting to #{host}:#{port}"
|
||||||
db = Connection.new(host, port).db('ruby-mongo-index_test')
|
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"
|
puts ">> Dropping collection test"
|
||||||
begin
|
begin
|
||||||
res = db.drop_collection('test')
|
res = db.drop_collection('test')
|
||||||
|
@ -43,8 +44,8 @@ coll.insert(arr)
|
||||||
puts "inserted"
|
puts "inserted"
|
||||||
|
|
||||||
puts ">> Creating index"
|
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}"
|
puts "created index: #{res.inspect}"
|
||||||
# ============================ Mongo Log ============================
|
# ============================ Mongo Log ============================
|
||||||
# Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index
|
# 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"
|
puts ">> Dropping index"
|
||||||
begin
|
begin
|
||||||
res = coll.drop_index "all_1"
|
res = coll.drop_index "number_1_rndm_1_msg_1"
|
||||||
puts "dropped : #{res.inspect}"
|
puts "dropped : #{res.inspect}"
|
||||||
rescue => e
|
rescue => e
|
||||||
puts "Error: #{e.errmsg}"
|
puts "Error: #{e.errmsg}"
|
||||||
|
|
|
@ -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'
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
|
|
@ -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'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
|
||||||
|
|
|
@ -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'
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
|
|
@ -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'
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
|
|
@ -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'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue