mongo 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +124 -111
- data/Rakefile +9 -325
- data/VERSION +1 -0
- data/bin/mongo_console +4 -4
- data/examples/admin.rb +43 -0
- data/examples/capped.rb +22 -0
- data/examples/cursor.rb +48 -0
- data/examples/gridfs.rb +44 -0
- data/examples/index_test.rb +126 -0
- data/examples/info.rb +31 -0
- data/examples/queries.rb +74 -0
- data/examples/replica_set.rb +26 -0
- data/examples/simple.rb +25 -0
- data/examples/strict.rb +35 -0
- data/examples/types.rb +36 -0
- data/{test/load → examples/web}/thin/load.rb +3 -1
- data/{test/load → examples/web}/unicorn/load.rb +5 -3
- data/lib/mongo.rb +8 -10
- data/lib/mongo/collection.rb +134 -114
- data/lib/mongo/cursor.rb +21 -14
- data/lib/mongo/db.rb +30 -28
- data/lib/mongo/exceptions.rb +1 -1
- data/lib/mongo/gridfs/grid.rb +8 -7
- data/lib/mongo/gridfs/grid_ext.rb +1 -1
- data/lib/mongo/gridfs/grid_file_system.rb +6 -5
- data/lib/mongo/gridfs/grid_io.rb +22 -19
- data/lib/mongo/legacy.rb +82 -0
- data/lib/mongo/{connection.rb → mongo_client.rb} +82 -61
- data/lib/mongo/{repl_set_connection.rb → mongo_replica_set_client.rb} +54 -39
- data/lib/mongo/{sharded_connection.rb → mongo_sharded_client.rb} +9 -9
- data/lib/mongo/networking.rb +25 -20
- data/lib/mongo/util/conversions.rb +1 -1
- data/lib/mongo/util/core_ext.rb +1 -1
- data/lib/mongo/util/logging.rb +20 -4
- data/lib/mongo/util/node.rb +16 -16
- data/lib/mongo/util/pool.rb +56 -27
- data/lib/mongo/util/pool_manager.rb +28 -27
- data/lib/mongo/util/server_version.rb +1 -1
- data/lib/mongo/util/sharding_pool_manager.rb +8 -8
- data/lib/mongo/util/ssl_socket.rb +1 -5
- data/lib/mongo/util/support.rb +24 -8
- data/lib/mongo/util/tcp_socket.rb +0 -4
- data/lib/mongo/util/uri_parser.rb +54 -38
- data/lib/mongo/util/write_concern.rb +67 -0
- data/mongo.gemspec +21 -32
- data/test/auxillary/{1.4_features.rb → 1.4_feature_test.rb} +4 -5
- data/test/auxillary/authentication_test.rb +18 -20
- data/test/auxillary/autoreconnect_test.rb +3 -5
- data/test/auxillary/fork_test.rb +5 -7
- data/test/auxillary/repl_set_auth_test.rb +13 -15
- data/test/auxillary/slave_connection_test.rb +8 -7
- data/test/auxillary/threaded_authentication_test.rb +15 -17
- data/test/bson/binary_test.rb +1 -1
- data/test/bson/bson_test.rb +60 -36
- data/test/bson/byte_buffer_test.rb +1 -1
- data/test/bson/hash_with_indifferent_access_test.rb +2 -2
- data/test/bson/json_test.rb +1 -2
- data/test/bson/object_id_test.rb +1 -2
- data/test/bson/ordered_hash_test.rb +1 -1
- data/test/bson/timestamp_test.rb +1 -1
- data/test/{collection_test.rb → functional/collection_test.rb} +57 -57
- data/test/{connection_test.rb → functional/connection_test.rb} +75 -89
- data/test/{conversions_test.rb → functional/conversions_test.rb} +1 -1
- data/test/{cursor_fail_test.rb → functional/cursor_fail_test.rb} +3 -29
- data/test/{cursor_message_test.rb → functional/cursor_message_test.rb} +1 -1
- data/test/{cursor_test.rb → functional/cursor_test.rb} +5 -1
- data/test/{db_api_test.rb → functional/db_api_test.rb} +8 -9
- data/test/{db_connection_test.rb → functional/db_connection_test.rb} +3 -5
- data/test/{db_test.rb → functional/db_test.rb} +13 -13
- data/test/{grid_file_system_test.rb → functional/grid_file_system_test.rb} +2 -2
- data/test/{grid_io_test.rb → functional/grid_io_test.rb} +6 -6
- data/test/{grid_test.rb → functional/grid_test.rb} +4 -10
- data/test/{pool_test.rb → functional/pool_test.rb} +1 -1
- data/test/functional/safe_test.rb +84 -0
- data/test/{support_test.rb → functional/support_test.rb} +1 -1
- data/test/{threading_test.rb → functional/threading_test.rb} +9 -9
- data/test/{timeout_test.rb → functional/timeout_test.rb} +1 -1
- data/test/{uri_test.rb → functional/uri_test.rb} +1 -1
- data/test/functional/write_concern_test.rb +104 -0
- data/test/replica_set/basic_test.rb +139 -0
- data/test/replica_set/client_test.rb +255 -0
- data/test/replica_set/complex_connect_test.rb +62 -0
- data/test/replica_set/connection_test.rb +255 -0
- data/test/{replica_sets → replica_set}/count_test.rb +17 -14
- data/test/replica_set/cursor_test.rb +75 -0
- data/test/{replica_sets → replica_set}/insert_test.rb +19 -16
- data/test/replica_set/query_test.rb +64 -0
- data/test/replica_set/refresh_test.rb +153 -0
- data/test/{replica_sets → replica_set}/replication_ack_test.rb +21 -17
- data/test/sharded_cluster/basic_test.rb +31 -50
- data/test/support/hash_with_indifferent_access.rb +1 -1
- data/test/test_helper.rb +56 -9
- data/test/threading/threading_with_large_pool_test.rb +8 -8
- data/test/tools/mongo_config.rb +270 -58
- data/test/tools/mongo_config_test.rb +146 -0
- data/test/unit/client_test.rb +230 -0
- data/test/unit/collection_test.rb +45 -32
- data/test/unit/connection_test.rb +82 -74
- data/test/unit/cursor_test.rb +14 -6
- data/test/unit/db_test.rb +8 -8
- data/test/unit/grid_test.rb +11 -11
- data/test/unit/node_test.rb +24 -24
- data/test/unit/pool_manager_test.rb +13 -13
- data/test/unit/pool_test.rb +1 -1
- data/test/unit/read_test.rb +21 -26
- data/test/unit/safe_test.rb +52 -33
- data/test/unit/util_test.rb +55 -0
- data/test/unit/write_concern_test.rb +161 -0
- metadata +158 -171
- data/docs/CREDITS.md +0 -123
- data/docs/FAQ.md +0 -116
- data/docs/GRID_FS.md +0 -158
- data/docs/HISTORY.md +0 -392
- data/docs/READ_PREFERENCE.md +0 -99
- data/docs/RELEASES.md +0 -54
- data/docs/REPLICA_SETS.md +0 -113
- data/docs/TAILABLE_CURSORS.md +0 -51
- data/docs/TUTORIAL.md +0 -356
- data/docs/WRITE_CONCERN.md +0 -31
- data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
- data/lib/mongo/version.rb +0 -3
- data/test/bson/test_helper.rb +0 -30
- data/test/replica_sets/basic_test.rb +0 -119
- data/test/replica_sets/complex_connect_test.rb +0 -57
- data/test/replica_sets/complex_read_preference_test.rb +0 -237
- data/test/replica_sets/connect_test.rb +0 -156
- data/test/replica_sets/cursor_test.rb +0 -70
- data/test/replica_sets/pooled_insert_test.rb +0 -57
- data/test/replica_sets/query_test.rb +0 -50
- data/test/replica_sets/read_preference_test.rb +0 -234
- data/test/replica_sets/refresh_test.rb +0 -156
- data/test/replica_sets/refresh_with_threads_test.rb +0 -60
- data/test/replica_sets/rs_test_helper.rb +0 -39
- data/test/safe_test.rb +0 -68
- data/test/sharded_cluster/mongo_config_test.rb +0 -126
- data/test/sharded_cluster/sc_test_helper.rb +0 -39
- data/test/tools/repl_set_manager.rb +0 -418
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.8.0
|
data/bin/mongo_console
CHANGED
@@ -10,12 +10,12 @@ require 'mongo'
|
|
10
10
|
include Mongo
|
11
11
|
|
12
12
|
host = org_argv[0] || ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
13
|
-
port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
13
|
+
port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
|
14
14
|
dbnm = org_argv[2] || ENV['MONGO_RUBY_DRIVER_DB'] || 'ruby-mongo-console'
|
15
15
|
|
16
|
-
puts "Connecting to #{host}:#{port} (
|
17
|
-
|
18
|
-
DB =
|
16
|
+
puts "Connecting to #{host}:#{port} (CLIENT) on with database #{dbnm} (DB)"
|
17
|
+
CLIENT = MongoClient.new(host, port)
|
18
|
+
DB = CLIENT.db(dbnm)
|
19
19
|
|
20
20
|
puts "Starting IRB session..."
|
21
21
|
IRB.start(__FILE__)
|
data/examples/admin.rb
ADDED
@@ -0,0 +1,43 @@
|
|
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'] || MongoClient::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
client = MongoClient.new(host, port)
|
13
|
+
db = client.db('ruby-mongo-examples')
|
14
|
+
coll = db.create_collection('test')
|
15
|
+
|
16
|
+
# Erase all records from collection, if any
|
17
|
+
coll.remove
|
18
|
+
|
19
|
+
admin = client['admin']
|
20
|
+
|
21
|
+
# Profiling level set/get
|
22
|
+
puts "Profiling level: #{admin.profiling_level}"
|
23
|
+
|
24
|
+
# Start profiling everything
|
25
|
+
admin.profiling_level = :all
|
26
|
+
|
27
|
+
# Read records, creating a profiling event
|
28
|
+
coll.find().to_a
|
29
|
+
|
30
|
+
# Stop profiling
|
31
|
+
admin.profiling_level = :off
|
32
|
+
|
33
|
+
# Print all profiling info
|
34
|
+
pp admin.profiling_info
|
35
|
+
|
36
|
+
# Validate returns a hash if all is well and
|
37
|
+
# raises an exception if there is a problem.
|
38
|
+
info = db.validate_collection(coll.name)
|
39
|
+
puts "valid = #{info['ok']}"
|
40
|
+
puts info['result']
|
41
|
+
|
42
|
+
# Destroy the collection
|
43
|
+
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'] || MongoClient::DEFAULT_PORT
|
8
|
+
|
9
|
+
puts "Connecting to #{host}:#{port}"
|
10
|
+
db = MongoClient.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'] || MongoClient::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = MongoClient.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,44 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
def assert
|
3
|
+
raise "Failed!" unless yield
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'mongo'
|
7
|
+
include Mongo
|
8
|
+
|
9
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
10
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
|
11
|
+
|
12
|
+
puts "Connecting to #{host}:#{port}"
|
13
|
+
db = MongoClient.new(host, port).db('ruby-mongo-examples')
|
14
|
+
|
15
|
+
data = "hello, world!"
|
16
|
+
|
17
|
+
grid = Grid.new(db)
|
18
|
+
|
19
|
+
# Write a new file. data can be a string or an io object responding to #read.
|
20
|
+
id = grid.put(data, :filename => 'hello.txt')
|
21
|
+
|
22
|
+
# Read it and print out the contents
|
23
|
+
file = grid.get(id)
|
24
|
+
puts file.read
|
25
|
+
|
26
|
+
# Delete the file
|
27
|
+
grid.delete(id)
|
28
|
+
|
29
|
+
begin
|
30
|
+
grid.get(id)
|
31
|
+
rescue => e
|
32
|
+
assert {e.class == Mongo::GridError}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Metadata
|
36
|
+
id = grid.put(data, :filename => 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
|
37
|
+
file = grid.get(id)
|
38
|
+
|
39
|
+
p file.content_type
|
40
|
+
p file.metadata.inspect
|
41
|
+
p file.chunk_size
|
42
|
+
p file.file_length
|
43
|
+
p file.filename
|
44
|
+
p file.data
|
@@ -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'] || MongoClient::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts ">> Connecting to #{host}:#{port}"
|
11
|
+
db = MongoClient.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'] || MongoClient::DEFAULT_PORT
|
9
|
+
|
10
|
+
puts "Connecting to #{host}:#{port}"
|
11
|
+
db = MongoClient.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
|
+
coll.create_index('a')
|
28
|
+
p db.index_information('test')
|
29
|
+
|
30
|
+
# Destroy the collection
|
31
|
+
coll.drop
|
data/examples/queries.rb
ADDED
@@ -0,0 +1,74 @@
|
|
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'] || MongoClient::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = MongoClient.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
|
+
coll.insert('c' => 'foo')
|
23
|
+
coll.insert('c' => 'bar')
|
24
|
+
|
25
|
+
# Count.
|
26
|
+
puts "There are #{coll.count} records."
|
27
|
+
|
28
|
+
# Find all records. find() returns a Cursor.
|
29
|
+
puts "Find all records:"
|
30
|
+
pp cursor = coll.find.to_a
|
31
|
+
|
32
|
+
# Print them. Note that all records have an _id automatically added by the
|
33
|
+
# database. See pk.rb for an example of how to use a primary key factory to
|
34
|
+
# generate your own values for _id.
|
35
|
+
puts "Print each document individually:"
|
36
|
+
pp cursor.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
|
+
puts "Find one record:"
|
41
|
+
pp coll.find('a' => 1).to_a
|
42
|
+
|
43
|
+
# Find records sort by 'a', skip 1, limit 2 records.
|
44
|
+
# Sort can be single name, array, or hash.
|
45
|
+
puts "Skip 1, limit 2, sort by 'a':"
|
46
|
+
pp coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'}).to_a
|
47
|
+
|
48
|
+
# Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
|
49
|
+
coll.find({'a' => {'$gt' => 1}})
|
50
|
+
coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
|
51
|
+
|
52
|
+
# Find all records with 'a' in a set of values.
|
53
|
+
puts "Find all records where a is $in [1, 2]:"
|
54
|
+
pp coll.find('a' => {'$in' => [1,2]}).to_a
|
55
|
+
|
56
|
+
puts "Find by regex:"
|
57
|
+
pp coll.find({'c' => /f/}).to_a
|
58
|
+
|
59
|
+
# Print query explanation
|
60
|
+
puts "Print an explain:"
|
61
|
+
pp coll.find({'c' => /f/}).explain
|
62
|
+
|
63
|
+
# Use a hint with a query. Need an index. Hints can be stored with the
|
64
|
+
# collection, in which case they will be used with all queries, or they can be
|
65
|
+
# specified per query, in which case that hint overrides the hint associated
|
66
|
+
# with the collection if any.
|
67
|
+
coll.create_index('c')
|
68
|
+
coll.hint = 'c'
|
69
|
+
|
70
|
+
puts "Print an explain with index:"
|
71
|
+
pp coll.find('c' => /[f|b]/).explain
|
72
|
+
|
73
|
+
puts "Print an explain with natural order hint:"
|
74
|
+
pp coll.find({'c' => /[f|b]/}, :hint => '$natural').explain
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# This code assumes a running replica set with at least one node at localhost:27017.
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
cons = []
|
7
|
+
|
8
|
+
10.times do
|
9
|
+
cons << MongoReplicaSetClient(['localhost:27017'], :read => :secondary)
|
10
|
+
end
|
11
|
+
|
12
|
+
ports = cons.map do |con|
|
13
|
+
con.read_pool.port
|
14
|
+
end
|
15
|
+
|
16
|
+
puts "These ten connections will read from the following ports:"
|
17
|
+
p ports
|
18
|
+
|
19
|
+
cons[rand(10)]['foo']['bar'].remove
|
20
|
+
100.times do |n|
|
21
|
+
cons[rand(10)]['foo']['bar'].insert({:a => n})
|
22
|
+
end
|
23
|
+
|
24
|
+
100.times do |n|
|
25
|
+
p cons[rand(10)]['foo']['bar'].find_one({:a => n})
|
26
|
+
end
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mongo'
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
|
8
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
9
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
|
10
|
+
|
11
|
+
puts "Connecting to #{host}:#{port}"
|
12
|
+
db = MongoClient.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
|
+
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
22
|
+
coll.find().each { |doc| puts doc.inspect }
|
23
|
+
|
24
|
+
# Destroy the collection
|
25
|
+
coll.drop
|