kbaum-mongo 0.18.3p
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +202 -0
- data/README.rdoc +339 -0
- data/Rakefile +138 -0
- data/bin/bson_benchmark.rb +59 -0
- data/bin/fail_if_no_c.rb +11 -0
- data/examples/admin.rb +42 -0
- data/examples/capped.rb +22 -0
- data/examples/cursor.rb +48 -0
- data/examples/gridfs.rb +88 -0
- data/examples/index_test.rb +126 -0
- data/examples/info.rb +31 -0
- data/examples/queries.rb +70 -0
- data/examples/simple.rb +24 -0
- data/examples/strict.rb +35 -0
- data/examples/types.rb +36 -0
- data/lib/mongo/collection.rb +609 -0
- data/lib/mongo/connection.rb +672 -0
- data/lib/mongo/cursor.rb +403 -0
- data/lib/mongo/db.rb +555 -0
- data/lib/mongo/exceptions.rb +66 -0
- data/lib/mongo/gridfs/chunk.rb +91 -0
- data/lib/mongo/gridfs/grid.rb +79 -0
- data/lib/mongo/gridfs/grid_file_system.rb +101 -0
- data/lib/mongo/gridfs/grid_io.rb +338 -0
- data/lib/mongo/gridfs/grid_store.rb +580 -0
- data/lib/mongo/gridfs.rb +25 -0
- data/lib/mongo/types/binary.rb +52 -0
- data/lib/mongo/types/code.rb +36 -0
- data/lib/mongo/types/dbref.rb +40 -0
- data/lib/mongo/types/min_max_keys.rb +58 -0
- data/lib/mongo/types/objectid.rb +180 -0
- data/lib/mongo/types/regexp_of_holding.rb +45 -0
- data/lib/mongo/util/bson_c.rb +18 -0
- data/lib/mongo/util/bson_ruby.rb +606 -0
- data/lib/mongo/util/byte_buffer.rb +222 -0
- data/lib/mongo/util/conversions.rb +87 -0
- data/lib/mongo/util/ordered_hash.rb +140 -0
- data/lib/mongo/util/server_version.rb +69 -0
- data/lib/mongo/util/support.rb +26 -0
- data/lib/mongo.rb +63 -0
- data/mongo-ruby-driver.gemspec +28 -0
- data/test/auxillary/autoreconnect_test.rb +42 -0
- data/test/binary_test.rb +15 -0
- data/test/bson_test.rb +427 -0
- data/test/byte_buffer_test.rb +81 -0
- data/test/chunk_test.rb +82 -0
- data/test/collection_test.rb +515 -0
- data/test/connection_test.rb +160 -0
- data/test/conversions_test.rb +120 -0
- data/test/cursor_test.rb +379 -0
- data/test/db_api_test.rb +780 -0
- data/test/db_connection_test.rb +16 -0
- data/test/db_test.rb +272 -0
- data/test/grid_file_system_test.rb +210 -0
- data/test/grid_io_test.rb +78 -0
- data/test/grid_store_test.rb +334 -0
- data/test/grid_test.rb +87 -0
- data/test/objectid_test.rb +125 -0
- data/test/ordered_hash_test.rb +172 -0
- data/test/replica/count_test.rb +34 -0
- data/test/replica/insert_test.rb +50 -0
- data/test/replica/pooled_insert_test.rb +54 -0
- data/test/replica/query_test.rb +39 -0
- data/test/slave_connection_test.rb +36 -0
- data/test/test_helper.rb +42 -0
- data/test/threading/test_threading_large_pool.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/unit/collection_test.rb +61 -0
- data/test/unit/connection_test.rb +117 -0
- data/test/unit/cursor_test.rb +93 -0
- data/test/unit/db_test.rb +98 -0
- metadata +127 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
require 'mongo'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
TRIALS = 100000
|
9
|
+
|
10
|
+
def encode(doc)
|
11
|
+
t0 = Time.new
|
12
|
+
b = BSON.new
|
13
|
+
TRIALS.times { |i|
|
14
|
+
b = BSON.new
|
15
|
+
b.serialize doc
|
16
|
+
}
|
17
|
+
print "took: #{Time.now.to_f - t0.to_f}\n"
|
18
|
+
return b
|
19
|
+
end
|
20
|
+
|
21
|
+
def decode(bson)
|
22
|
+
t0 = Time.new
|
23
|
+
doc = nil
|
24
|
+
TRIALS.times { |i|
|
25
|
+
doc = bson.deserialize
|
26
|
+
}
|
27
|
+
print "took: #{Time.now.to_f - t0.to_f}\n"
|
28
|
+
return doc
|
29
|
+
end
|
30
|
+
|
31
|
+
TEST_CASES = [{},
|
32
|
+
{
|
33
|
+
"hello" => "world"
|
34
|
+
},
|
35
|
+
{
|
36
|
+
"hello" => "world",
|
37
|
+
"mike" => "something",
|
38
|
+
"here's" => "another"
|
39
|
+
},
|
40
|
+
{
|
41
|
+
"int" => 200,
|
42
|
+
"bool" => true,
|
43
|
+
"an int" => 20,
|
44
|
+
"a bool" => false
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"this" => 5,
|
48
|
+
"is" => {"a" => true},
|
49
|
+
"big" => [true, 5.5],
|
50
|
+
"object" => nil
|
51
|
+
}]
|
52
|
+
|
53
|
+
TEST_CASES.each { |doc|
|
54
|
+
print "case #{doc.inspect}\n"
|
55
|
+
print "enc bson\n"
|
56
|
+
enc_bson = encode(doc)
|
57
|
+
print "dec bson\n"
|
58
|
+
raise "FAIL" unless doc == decode(enc_bson)
|
59
|
+
}
|
data/bin/fail_if_no_c.rb
ADDED
data/examples/admin.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = Mongo::Connection.new(host, port).db('ruby-mongo-examples')
|
13
|
+
coll = db.create_collection('test')
|
14
|
+
|
15
|
+
# Erase all records from collection, if any
|
16
|
+
coll.remove
|
17
|
+
|
18
|
+
admin = db.admin
|
19
|
+
|
20
|
+
# Profiling level set/get
|
21
|
+
puts "Profiling level: #{admin.profiling_level}"
|
22
|
+
|
23
|
+
# Start profiling everything
|
24
|
+
admin.profiling_level = :all
|
25
|
+
|
26
|
+
# Read records, creating a profiling event
|
27
|
+
coll.find().to_a
|
28
|
+
|
29
|
+
# Stop profiling
|
30
|
+
admin.profiling_level = :off
|
31
|
+
|
32
|
+
# Print all profiling info
|
33
|
+
pp admin.profiling_info
|
34
|
+
|
35
|
+
# Validate returns a hash if all is well and
|
36
|
+
# raises an exception if there is a problem.
|
37
|
+
info = admin.validate_collection(coll.name)
|
38
|
+
puts "valid = #{info['ok']}"
|
39
|
+
puts info['result']
|
40
|
+
|
41
|
+
# Destroy the collection
|
42
|
+
coll.drop
|
data/examples/capped.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
7
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
11
|
+
db.drop_collection('test')
|
12
|
+
|
13
|
+
# A capped collection has a max size and, optionally, a max number of records.
|
14
|
+
# Old records get pushed out by new ones once the size or max num records is reached.
|
15
|
+
coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
|
16
|
+
|
17
|
+
100.times { |i| coll.insert('a' => i+1) }
|
18
|
+
|
19
|
+
# We will only see the last 12 records
|
20
|
+
coll.find().each { |row| p row }
|
21
|
+
|
22
|
+
coll.drop
|
data/examples/cursor.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
13
|
+
coll = db.collection('test')
|
14
|
+
|
15
|
+
# Erase all records from collection, if any
|
16
|
+
coll.remove
|
17
|
+
|
18
|
+
# Insert 3 records
|
19
|
+
3.times { |i| coll.insert({'a' => i+1}) }
|
20
|
+
|
21
|
+
# Cursors don't run their queries until you actually attempt to retrieve data
|
22
|
+
# from them.
|
23
|
+
|
24
|
+
# Find returns a Cursor, which is Enumerable. You can iterate:
|
25
|
+
coll.find().each { |row| pp row }
|
26
|
+
|
27
|
+
# You can turn it into an array:
|
28
|
+
array = coll.find().to_a
|
29
|
+
|
30
|
+
# You can iterate after turning it into an array (the cursor will iterate over
|
31
|
+
# the copy of the array that it saves internally.)
|
32
|
+
cursor = coll.find()
|
33
|
+
array = cursor.to_a
|
34
|
+
cursor.each { |row| pp row }
|
35
|
+
|
36
|
+
# You can get the next object
|
37
|
+
first_object = coll.find().next_document
|
38
|
+
|
39
|
+
# next_document returns nil if there are no more objects that match
|
40
|
+
cursor = coll.find()
|
41
|
+
obj = cursor.next_document
|
42
|
+
while obj
|
43
|
+
pp obj
|
44
|
+
obj = cursor.next_document
|
45
|
+
end
|
46
|
+
|
47
|
+
# Destroy the collection
|
48
|
+
coll.drop
|
data/examples/gridfs.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
require 'mongo/gridfs'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
include GridFS
|
8
|
+
|
9
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
10
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
11
|
+
|
12
|
+
puts "Connecting to #{host}:#{port}"
|
13
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
14
|
+
|
15
|
+
def dump(db, fname)
|
16
|
+
GridStore.open(db, fname, 'r') { |f| puts f.read }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Write a new file
|
20
|
+
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
21
|
+
|
22
|
+
# Read it and print out the contents
|
23
|
+
dump(db, 'foobar')
|
24
|
+
|
25
|
+
# Append more data
|
26
|
+
GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
|
27
|
+
dump(db, 'foobar')
|
28
|
+
|
29
|
+
# Overwrite
|
30
|
+
GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" }
|
31
|
+
dump(db, 'foobar')
|
32
|
+
|
33
|
+
# File existence tests
|
34
|
+
puts "File 'foobar' exists: #{GridStore.exist?(db, 'foobar')}"
|
35
|
+
puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
|
36
|
+
|
37
|
+
# Read with offset (uses seek)
|
38
|
+
puts GridStore.read(db, 'foobar', 6, 7)
|
39
|
+
|
40
|
+
# Rewind/seek/tell
|
41
|
+
GridStore.open(db, 'foobar', 'w') { |f|
|
42
|
+
f.write "hello, world!"
|
43
|
+
f.rewind
|
44
|
+
f.write "xyzzz"
|
45
|
+
puts f.tell # => 5
|
46
|
+
f.seek(4)
|
47
|
+
f.write('y')
|
48
|
+
}
|
49
|
+
dump(db, 'foobar') # => 'xyzzy'
|
50
|
+
|
51
|
+
# Unlink (delete)
|
52
|
+
GridStore.unlink(db, 'foobar')
|
53
|
+
puts "File 'foobar' exists after delete: #{GridStore.exist?(db, 'foobar')}"
|
54
|
+
|
55
|
+
# Metadata
|
56
|
+
GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
57
|
+
GridStore.open(db, 'foobar', 'r') { |f|
|
58
|
+
puts f.content_type
|
59
|
+
puts f.upload_date
|
60
|
+
puts f.chunk_size
|
61
|
+
puts f.metadata.inspect
|
62
|
+
}
|
63
|
+
|
64
|
+
# Add some metadata; change content type
|
65
|
+
GridStore.open(db, 'foobar', 'w+') { |f|
|
66
|
+
f.content_type = 'text/xml'
|
67
|
+
f.metadata = {'a' => 1}
|
68
|
+
}
|
69
|
+
# Print it
|
70
|
+
GridStore.open(db, 'foobar', 'r') { |f|
|
71
|
+
puts f.content_type
|
72
|
+
puts f.upload_date
|
73
|
+
puts f.chunk_size
|
74
|
+
puts f.metadata.inspect
|
75
|
+
}
|
76
|
+
|
77
|
+
# You can also set metadata when initially writing the file. Setting :root
|
78
|
+
# means that the file and its chunks are stored in a different root
|
79
|
+
# collection: instead of gridfs.files and gridfs.chunks, here we use
|
80
|
+
# my_files.files and my_files.chunks.
|
81
|
+
GridStore.open(db, 'foobar', 'w',
|
82
|
+
:content_type => 'text/plain',
|
83
|
+
:metadata => {'a' => 1},
|
84
|
+
:chunk_size => 1024 * 4,
|
85
|
+
:root => 'my_files') { |f|
|
86
|
+
f.puts 'hello, world'
|
87
|
+
}
|
88
|
+
|
@@ -0,0 +1,126 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts ">> Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-index_test')
|
12
|
+
|
13
|
+
class Exception
|
14
|
+
def errmsg
|
15
|
+
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
puts ">> Dropping collection test"
|
20
|
+
begin
|
21
|
+
res = db.drop_collection('test')
|
22
|
+
puts "dropped : #{res.inspect}"
|
23
|
+
rescue => e
|
24
|
+
puts "Error: #{e.errmsg}"
|
25
|
+
end
|
26
|
+
|
27
|
+
puts ">> Creating collection test"
|
28
|
+
begin
|
29
|
+
coll = db.collection('test')
|
30
|
+
puts "created : #{coll.inspect}"
|
31
|
+
rescue => e
|
32
|
+
puts "Error: #{e.errmsg}"
|
33
|
+
end
|
34
|
+
|
35
|
+
OBJS_COUNT = 100
|
36
|
+
|
37
|
+
puts ">> Generating test data"
|
38
|
+
msgs = %w{hola hello aloha ciao}
|
39
|
+
arr = (0...OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
|
40
|
+
puts "generated"
|
41
|
+
|
42
|
+
puts ">> Inserting data (#{arr.size})"
|
43
|
+
coll.insert(arr)
|
44
|
+
puts "inserted"
|
45
|
+
|
46
|
+
puts ">> Creating index"
|
47
|
+
#res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1
|
48
|
+
res = coll.create_index [[:number, 1], [:rndm, 1], [:msg, 1]]
|
49
|
+
puts "created index: #{res.inspect}"
|
50
|
+
# ============================ Mongo Log ============================
|
51
|
+
# Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index
|
52
|
+
# ***
|
53
|
+
# Bad data or size in BSONElement::size()
|
54
|
+
# bad type:30
|
55
|
+
# totalsize:11 fieldnamesize:4
|
56
|
+
# lastrec:
|
57
|
+
# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Assertion failure false jsobj.cpp a0
|
58
|
+
# Fri Dec 5 14:45:02 database: ruby-mongo-console op:7d2 0
|
59
|
+
# Fri Dec 5 14:45:02 ns: ruby-mongo-console.system.indexes
|
60
|
+
|
61
|
+
puts ">> Gathering index information"
|
62
|
+
begin
|
63
|
+
res = coll.index_information
|
64
|
+
puts "index_information : #{res.inspect}"
|
65
|
+
rescue => e
|
66
|
+
puts "Error: #{e.errmsg}"
|
67
|
+
end
|
68
|
+
# ============================ Console Output ============================
|
69
|
+
# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
|
70
|
+
# from ./bin/../lib/mongo/db.rb:135:in `index_information'
|
71
|
+
# from (irb):11:in `collect'
|
72
|
+
# from ./bin/../lib/mongo/cursor.rb:47:in `each'
|
73
|
+
# from ./bin/../lib/mongo/db.rb:130:in `collect'
|
74
|
+
# from ./bin/../lib/mongo/db.rb:130:in `index_information'
|
75
|
+
# from ./bin/../lib/mongo/collection.rb:74:in `index_information'
|
76
|
+
# from (irb):11
|
77
|
+
|
78
|
+
puts ">> Dropping index"
|
79
|
+
begin
|
80
|
+
res = coll.drop_index "number_1_rndm_1_msg_1"
|
81
|
+
puts "dropped : #{res.inspect}"
|
82
|
+
rescue => e
|
83
|
+
puts "Error: #{e.errmsg}"
|
84
|
+
end
|
85
|
+
|
86
|
+
# ============================ Console Output ============================
|
87
|
+
# => {"nIndexesWas"=>2.0, "ok"=>1.0}
|
88
|
+
# ============================ Mongo Log ============================
|
89
|
+
# 0x41802a 0x411549 0x42bac6 0x42c1f6 0x42c55b 0x42e6f7 0x41631e 0x41a89d 0x41ade2 0x41b448 0x4650d2 0x4695ad
|
90
|
+
# db/db(_Z12sayDbContextPKc+0x17a) [0x41802a]
|
91
|
+
# db/db(_Z8assertedPKcS0_j+0x9) [0x411549]
|
92
|
+
# db/db(_ZNK11BSONElement4sizeEv+0x1f6) [0x42bac6]
|
93
|
+
# db/db(_ZN7BSONObj8getFieldEPKc+0xa6) [0x42c1f6]
|
94
|
+
# db/db(_ZN7BSONObj14getFieldDottedEPKc+0x11b) [0x42c55b]
|
95
|
+
# db/db(_ZN7BSONObj19extractFieldsDottedES_R14BSONObjBuilder+0x87) [0x42e6f7]
|
96
|
+
# db/db(_ZN12IndexDetails17getKeysFromObjectER7BSONObjRSt3setIS0_St4lessIS0_ESaIS0_EE+0x24e) [0x41631e]
|
97
|
+
# db/db(_Z12_indexRecordR12IndexDetailsR7BSONObj7DiskLoc+0x5d) [0x41a89d]
|
98
|
+
# db/db(_Z18addExistingToIndexPKcR12IndexDetails+0xb2) [0x41ade2]
|
99
|
+
# db/db(_ZN11DataFileMgr6insertEPKcPKvib+0x508) [0x41b448]
|
100
|
+
# db/db(_Z14receivedInsertR7MessageRSt18basic_stringstreamIcSt11char_traitsIcESaIcEE+0x112) [0x4650d2]
|
101
|
+
# db/db(_Z10connThreadv+0xb4d) [0x4695ad]
|
102
|
+
# Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Caught Assertion insert, continuing
|
103
|
+
# Fri Dec 5 14:47:59 CMD: deleteIndexes ruby-mongo-console.test
|
104
|
+
# d->nIndexes was 2
|
105
|
+
# alpha implementation, space not reclaimed
|
106
|
+
|
107
|
+
puts ">> Gathering index information"
|
108
|
+
begin
|
109
|
+
res = coll.index_information
|
110
|
+
puts "index_information : #{res.inspect}"
|
111
|
+
rescue => e
|
112
|
+
puts "Error: #{e.errmsg}"
|
113
|
+
end
|
114
|
+
# ============================ Console Output ============================
|
115
|
+
# RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
|
116
|
+
# from ./bin/../lib/mongo/db.rb:135:in `index_information'
|
117
|
+
# from (irb):15:in `collect'
|
118
|
+
# from ./bin/../lib/mongo/cursor.rb:47:in `each'
|
119
|
+
# from ./bin/../lib/mongo/db.rb:130:in `collect'
|
120
|
+
# from ./bin/../lib/mongo/db.rb:130:in `index_information'
|
121
|
+
# from ./bin/../lib/mongo/collection.rb:74:in `index_information'
|
122
|
+
# from (irb):15
|
123
|
+
|
124
|
+
puts ">> Closing connection"
|
125
|
+
db.close
|
126
|
+
puts "closed"
|
data/examples/info.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
12
|
+
coll = db.collection('test')
|
13
|
+
|
14
|
+
# Erase all records from collection, if any
|
15
|
+
coll.remove
|
16
|
+
|
17
|
+
# Insert 3 records
|
18
|
+
3.times { |i| coll.insert({'a' => i+1}) }
|
19
|
+
|
20
|
+
# Collection names in database
|
21
|
+
p db.collection_names
|
22
|
+
|
23
|
+
# More information about each collection
|
24
|
+
p db.collections_info
|
25
|
+
|
26
|
+
# Index information
|
27
|
+
db.create_index('test', 'a')
|
28
|
+
p db.index_information('test')
|
29
|
+
|
30
|
+
# Destroy the collection
|
31
|
+
coll.drop
|
data/examples/queries.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
13
|
+
coll = db.collection('test')
|
14
|
+
|
15
|
+
# Remove all records, if any
|
16
|
+
coll.remove
|
17
|
+
|
18
|
+
# Insert three records
|
19
|
+
coll.insert('a' => 1)
|
20
|
+
coll.insert('a' => 2)
|
21
|
+
coll.insert('b' => 3)
|
22
|
+
|
23
|
+
# Count.
|
24
|
+
puts "There are #{coll.count()} records."
|
25
|
+
|
26
|
+
# Find all records. find() returns a Cursor.
|
27
|
+
cursor = coll.find()
|
28
|
+
|
29
|
+
# Print them. Note that all records have an _id automatically added by the
|
30
|
+
# database. See pk.rb for an example of how to use a primary key factory to
|
31
|
+
# generate your own values for _id.
|
32
|
+
cursor.each { |row| pp row }
|
33
|
+
|
34
|
+
# Cursor has a to_a method that slurps all records into memory.
|
35
|
+
rows = coll.find().to_a
|
36
|
+
rows.each { |row| pp row }
|
37
|
+
|
38
|
+
# See Collection#find. From now on in this file, we won't be printing the
|
39
|
+
# records we find.
|
40
|
+
coll.find('a' => 1)
|
41
|
+
|
42
|
+
# Find records sort by 'a', skip 1, limit 2 records.
|
43
|
+
# Sort can be single name, array, or hash.
|
44
|
+
coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'})
|
45
|
+
|
46
|
+
# Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
|
47
|
+
coll.find({'a' => {'$gt' => 1}})
|
48
|
+
coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
|
49
|
+
|
50
|
+
# Find all records with 'a' in a set of values.
|
51
|
+
coll.find('a' => {'$in' => [1,2]})
|
52
|
+
|
53
|
+
# Find by regexp
|
54
|
+
coll.find('a' => /[1|2]/)
|
55
|
+
|
56
|
+
# Print query explanation
|
57
|
+
pp coll.find('a' => /[1|2]/).explain()
|
58
|
+
|
59
|
+
# Use a hint with a query. Need an index. Hints can be stored with the
|
60
|
+
# collection, in which case they will be used with all queries, or they can be
|
61
|
+
# specified per query, in which case that hint overrides the hint associated
|
62
|
+
# with the collection if any.
|
63
|
+
coll.create_index('a')
|
64
|
+
coll.hint = 'a'
|
65
|
+
|
66
|
+
# You will see a different explanation now that the hint is in place
|
67
|
+
pp coll.find('a' => /[1|2]/).explain()
|
68
|
+
|
69
|
+
# Override hint for single query
|
70
|
+
coll.find({'a' => 1}, :hint => 'b')
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
12
|
+
coll = db.collection('test')
|
13
|
+
|
14
|
+
# Erase all records from collection, if any
|
15
|
+
coll.remove
|
16
|
+
|
17
|
+
# Insert 3 records
|
18
|
+
3.times { |i| coll.insert({'a' => i+1}) }
|
19
|
+
|
20
|
+
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
21
|
+
coll.find().each { |doc| puts doc.inspect }
|
22
|
+
|
23
|
+
# Destroy the collection
|
24
|
+
coll.drop
|
data/examples/strict.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
|
5
|
+
include Mongo
|
6
|
+
|
7
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
8
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
12
|
+
|
13
|
+
db.drop_collection('does-not-exist')
|
14
|
+
db.create_collection('test')
|
15
|
+
|
16
|
+
db.strict = true
|
17
|
+
|
18
|
+
begin
|
19
|
+
# Can't reference collection that does not exist
|
20
|
+
db.collection('does-not-exist')
|
21
|
+
puts "error: expected exception"
|
22
|
+
rescue => ex
|
23
|
+
puts "expected exception: #{ex}"
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
# Can't create collection that already exists
|
28
|
+
db.create_collection('test')
|
29
|
+
puts "error: expected exception"
|
30
|
+
rescue => ex
|
31
|
+
puts "expected exception: #{ex}"
|
32
|
+
end
|
33
|
+
|
34
|
+
db.strict = false
|
35
|
+
db.drop_collection('test')
|
data/examples/types.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'mongo'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = Connection.new(host, port).db('ruby-mongo-examples')
|
13
|
+
coll = db.collection('test')
|
14
|
+
|
15
|
+
# Remove all records, if any
|
16
|
+
coll.remove
|
17
|
+
|
18
|
+
# Insert record with all sorts of values
|
19
|
+
coll.insert('array' => [1, 2, 3],
|
20
|
+
'string' => 'hello',
|
21
|
+
'hash' => {'a' => 1, 'b' => 2},
|
22
|
+
'date' => Time.now, # milliseconds only; microseconds are not stored
|
23
|
+
'oid' => ObjectID.new,
|
24
|
+
'binary' => Binary.new([1, 2, 3]),
|
25
|
+
'int' => 42,
|
26
|
+
'float' => 33.33333,
|
27
|
+
'regex' => /foobar/i,
|
28
|
+
'boolean' => true,
|
29
|
+
'where' => Code.new('this.x == 3'),
|
30
|
+
'dbref' => DBRef.new(coll.name, ObjectID.new),
|
31
|
+
'null' => nil,
|
32
|
+
'symbol' => :zildjian)
|
33
|
+
|
34
|
+
pp coll.find().next_document
|
35
|
+
|
36
|
+
coll.remove
|