many examples
This commit is contained in:
parent
bd602ba369
commit
14210b0ca3
35
examples/admin.rb
Normal file
35
examples/admin.rb
Normal file
@ -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
|
@ -8,7 +8,7 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|||||||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
||||||
|
|
||||||
puts "Connecting to #{host}:#{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 = db.collection('test')
|
||||||
coll.clear
|
coll.clear
|
||||||
|
|
||||||
|
23
examples/capped.rb
Normal file
23
examples/capped.rb
Normal file
@ -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
|
@ -1,6 +1,3 @@
|
|||||||
require "rubygems"
|
|
||||||
require "benchwarmer"
|
|
||||||
|
|
||||||
class Exception
|
class Exception
|
||||||
def errmsg
|
def errmsg
|
||||||
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
||||||
@ -38,7 +35,7 @@ OBJS_COUNT = 100
|
|||||||
|
|
||||||
puts ">> Generating test data"
|
puts ">> Generating test data"
|
||||||
msgs = %w{hola hello aloha ciao}
|
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 "generated"
|
||||||
|
|
||||||
puts ">> Inserting data (#{arr.size})"
|
puts ">> Inserting data (#{arr.size})"
|
||||||
|
30
examples/info.rb
Normal file
30
examples/info.rb
Normal file
@ -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
|
69
examples/queries.rb
Normal file
69
examples/queries.rb
Normal file
@ -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')
|
@ -7,11 +7,17 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|||||||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
||||||
|
|
||||||
puts "Connecting to #{host}:#{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')
|
coll = db.collection('test')
|
||||||
|
|
||||||
|
# Erase all records from collection, if any
|
||||||
coll.clear
|
coll.clear
|
||||||
|
|
||||||
|
# Insert 3 records
|
||||||
3.times { |i| coll.insert({'a' => i+1}) }
|
3.times { |i| coll.insert({'a' => i+1}) }
|
||||||
|
|
||||||
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
||||||
coll.find().each { |doc| puts doc.inspect }
|
coll.find().each { |doc| puts doc.inspect }
|
||||||
|
|
||||||
|
# Destroy the collection
|
||||||
|
coll.drop
|
||||||
|
34
examples/strict.rb
Normal file
34
examples/strict.rb
Normal file
@ -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')
|
40
examples/types.rb
Normal file
40
examples/types.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user