jonbell-mongo 1.3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +190 -0
- data/README.md +333 -0
- data/Rakefile +215 -0
- data/bin/mongo_console +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +116 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +263 -0
- data/docs/RELEASES.md +33 -0
- data/docs/REPLICA_SETS.md +72 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo.rb +97 -0
- data/lib/mongo/collection.rb +895 -0
- data/lib/mongo/connection.rb +926 -0
- data/lib/mongo/cursor.rb +474 -0
- data/lib/mongo/db.rb +617 -0
- data/lib/mongo/exceptions.rb +71 -0
- data/lib/mongo/gridfs/grid.rb +107 -0
- data/lib/mongo/gridfs/grid_ext.rb +57 -0
- data/lib/mongo/gridfs/grid_file_system.rb +146 -0
- data/lib/mongo/gridfs/grid_io.rb +485 -0
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +356 -0
- data/lib/mongo/util/conversions.rb +89 -0
- data/lib/mongo/util/core_ext.rb +60 -0
- data/lib/mongo/util/pool.rb +177 -0
- data/lib/mongo/util/server_version.rb +71 -0
- data/lib/mongo/util/support.rb +82 -0
- data/lib/mongo/util/uri_parser.rb +185 -0
- data/mongo.gemspec +34 -0
- data/test/auxillary/1.4_features.rb +166 -0
- data/test/auxillary/authentication_test.rb +68 -0
- data/test/auxillary/autoreconnect_test.rb +41 -0
- data/test/auxillary/fork_test.rb +30 -0
- data/test/auxillary/repl_set_auth_test.rb +58 -0
- data/test/auxillary/slave_connection_test.rb +36 -0
- data/test/auxillary/threaded_authentication_test.rb +101 -0
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +654 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +154 -0
- data/test/bson/ordered_hash_test.rb +210 -0
- data/test/bson/timestamp_test.rb +24 -0
- data/test/collection_test.rb +910 -0
- data/test/connection_test.rb +324 -0
- data/test/conversions_test.rb +119 -0
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +483 -0
- data/test/db_api_test.rb +738 -0
- data/test/db_connection_test.rb +15 -0
- data/test/db_test.rb +315 -0
- data/test/grid_file_system_test.rb +259 -0
- data/test/grid_io_test.rb +209 -0
- data/test/grid_test.rb +258 -0
- data/test/load/thin/load.rb +24 -0
- data/test/load/unicorn/load.rb +23 -0
- data/test/replica_sets/connect_test.rb +112 -0
- data/test/replica_sets/connection_string_test.rb +32 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/replica_sets/insert_test.rb +53 -0
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +108 -0
- data/test/replica_sets/query_test.rb +51 -0
- data/test/replica_sets/replication_ack_test.rb +66 -0
- data/test/replica_sets/rs_test_helper.rb +27 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +186 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +18 -0
- data/test/test_helper.rb +102 -0
- data/test/threading/threading_with_large_pool_test.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/tools/auth_repl_set_manager.rb +14 -0
- data/test/tools/repl_set_manager.rb +266 -0
- data/test/unit/collection_test.rb +130 -0
- data/test/unit/connection_test.rb +85 -0
- data/test/unit/cursor_test.rb +109 -0
- data/test/unit/db_test.rb +94 -0
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +59 -0
- data/test/unit/safe_test.rb +125 -0
- data/test/uri_test.rb +91 -0
- metadata +224 -0
data/Rakefile
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
# -*- mode: ruby; -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rubygems/specification'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'rake'
|
6
|
+
require 'rake/testtask'
|
7
|
+
require 'rake/gempackagetask'
|
8
|
+
require 'rbconfig'
|
9
|
+
include Config
|
10
|
+
ENV['TEST_MODE'] = 'TRUE'
|
11
|
+
|
12
|
+
task :java do
|
13
|
+
Rake::Task['build:java'].invoke
|
14
|
+
Rake::Task['test:ruby'].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :build do
|
18
|
+
desc "Build the java extensions."
|
19
|
+
task :java do
|
20
|
+
puts "Building Java extensions..."
|
21
|
+
java_dir = File.join(File.dirname(__FILE__), 'ext', 'java')
|
22
|
+
jar_dir = File.join(java_dir, 'jar')
|
23
|
+
|
24
|
+
jruby_jar = File.join(jar_dir, 'jruby.jar')
|
25
|
+
mongo_jar = File.join(jar_dir, 'mongo-2.4.jar')
|
26
|
+
bson_jar = File.join(jar_dir, 'bson-2.2.jar')
|
27
|
+
|
28
|
+
src_base = File.join(java_dir, 'src')
|
29
|
+
|
30
|
+
system("javac -Xlint:unchecked -classpath #{jruby_jar}:#{mongo_jar}:#{bson_jar} #{File.join(src_base, 'org', 'jbson', '*.java')}")
|
31
|
+
system("cd #{src_base} && jar cf #{File.join(jar_dir, 'jbson.jar')} #{File.join('.', 'org', 'jbson', '*.class')}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Test the MongoDB Ruby driver."
|
36
|
+
task :test do
|
37
|
+
puts "\nTo test the driver with the C-extensions:\nrake test:c\n\n"
|
38
|
+
puts "To test the pure ruby driver: \nrake test:ruby\n\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
namespace :test do
|
42
|
+
|
43
|
+
desc "Test the driver with the C extension enabled."
|
44
|
+
task :c do
|
45
|
+
ENV['C_EXT'] = 'TRUE'
|
46
|
+
if ENV['TEST']
|
47
|
+
Rake::Task['test:functional'].invoke
|
48
|
+
else
|
49
|
+
Rake::Task['test:unit'].invoke
|
50
|
+
Rake::Task['test:functional'].invoke
|
51
|
+
Rake::Task['test:bson'].invoke
|
52
|
+
Rake::Task['test:pooled_threading'].invoke
|
53
|
+
Rake::Task['test:drop_databases'].invoke
|
54
|
+
end
|
55
|
+
ENV['C_EXT'] = nil
|
56
|
+
end
|
57
|
+
|
58
|
+
desc "Test the driver using pure ruby (no C extension)"
|
59
|
+
task :ruby do
|
60
|
+
ENV['C_EXT'] = nil
|
61
|
+
if ENV['TEST']
|
62
|
+
Rake::Task['test:functional'].invoke
|
63
|
+
else
|
64
|
+
Rake::Task['test:unit'].invoke
|
65
|
+
Rake::Task['test:functional'].invoke
|
66
|
+
Rake::Task['test:bson'].invoke
|
67
|
+
Rake::Task['test:pooled_threading'].invoke
|
68
|
+
Rake::Task['test:drop_databases'].invoke
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
desc "Run the replica set test suite"
|
73
|
+
Rake::TestTask.new(:rs) do |t|
|
74
|
+
t.test_files = FileList['test/replica_sets/*_test.rb']
|
75
|
+
t.verbose = true
|
76
|
+
t.ruby_opts << '-w'
|
77
|
+
end
|
78
|
+
|
79
|
+
Rake::TestTask.new(:unit) do |t|
|
80
|
+
t.test_files = FileList['test/unit/*_test.rb']
|
81
|
+
t.verbose = true
|
82
|
+
t.ruby_opts << '-w'
|
83
|
+
end
|
84
|
+
|
85
|
+
Rake::TestTask.new(:functional) do |t|
|
86
|
+
t.test_files = FileList['test/*_test.rb']
|
87
|
+
t.verbose = true
|
88
|
+
t.ruby_opts << '-w'
|
89
|
+
end
|
90
|
+
|
91
|
+
Rake::TestTask.new(:pooled_threading) do |t|
|
92
|
+
t.test_files = FileList['test/threading/*_test.rb']
|
93
|
+
t.verbose = true
|
94
|
+
t.ruby_opts << '-w'
|
95
|
+
end
|
96
|
+
|
97
|
+
Rake::TestTask.new(:auto_reconnect) do |t|
|
98
|
+
t.test_files = FileList['test/auxillary/autoreconnect_test.rb']
|
99
|
+
t.verbose = true
|
100
|
+
t.ruby_opts << '-w'
|
101
|
+
end
|
102
|
+
|
103
|
+
Rake::TestTask.new(:authentication) do |t|
|
104
|
+
t.test_files = FileList['test/auxillary/authentication_test.rb']
|
105
|
+
t.verbose = true
|
106
|
+
t.ruby_opts << '-w'
|
107
|
+
end
|
108
|
+
|
109
|
+
Rake::TestTask.new(:new_features) do |t|
|
110
|
+
t.test_files = FileList['test/auxillary/1.4_features.rb']
|
111
|
+
t.verbose = true
|
112
|
+
t.ruby_opts << '-w'
|
113
|
+
end
|
114
|
+
|
115
|
+
Rake::TestTask.new(:bson) do |t|
|
116
|
+
t.test_files = FileList['test/bson/*_test.rb']
|
117
|
+
t.verbose = true
|
118
|
+
t.ruby_opts << '-w'
|
119
|
+
end
|
120
|
+
|
121
|
+
task :drop_databases do |t|
|
122
|
+
puts "Dropping test databases..."
|
123
|
+
require './lib/mongo'
|
124
|
+
con = Mongo::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
125
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT)
|
126
|
+
con.database_names.each do |name|
|
127
|
+
con.drop_database(name) if name =~ /^ruby-test/
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
desc "Generate RDOC documentation"
|
133
|
+
task :rdoc do
|
134
|
+
version = eval(File.read("mongo.gemspec")).version
|
135
|
+
out = File.join('html', version.to_s)
|
136
|
+
FileUtils.rm_rf('html')
|
137
|
+
system "rdoc --main README.md --op #{out} --inline-source --quiet README.md `find lib -name '*.rb'`"
|
138
|
+
end
|
139
|
+
|
140
|
+
desc "Generate YARD documentation"
|
141
|
+
task :ydoc do
|
142
|
+
require File.join(File.dirname(__FILE__), 'lib', 'mongo')
|
143
|
+
out = File.join('ydoc', Mongo::VERSION)
|
144
|
+
FileUtils.rm_rf('ydoc')
|
145
|
+
system "yardoc lib/**/*.rb lib/mongo/**/*.rb lib/bson/**/*.rb -e yard/yard_ext.rb -p yard/templates -o #{out} --title MongoRuby-#{Mongo::VERSION} --files docs/TUTORIAL.md,docs/GridFS.md,docs/FAQ.md,docs/REPLICA_SETS.md,docs/WRITE_CONCERN.md,docs/HISTORY.md,docs/CREDITS.md,docs/RELEASES.md"
|
146
|
+
end
|
147
|
+
|
148
|
+
namespace :bamboo do
|
149
|
+
task :ci_reporter do
|
150
|
+
begin
|
151
|
+
require 'ci/reporter/rake/test_unit'
|
152
|
+
rescue LoadError
|
153
|
+
warn "Warning: Unable to load ci_reporter gem."
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
namespace :test do
|
158
|
+
task :ruby => [:ci_reporter, "ci:setup:testunit"] do
|
159
|
+
Rake::Task['test:ruby'].invoke
|
160
|
+
end
|
161
|
+
|
162
|
+
task :c => [:ci_reporter, "ci:setup:testunit"] do
|
163
|
+
Rake::Task['gem:install_extensions'].invoke
|
164
|
+
Rake::Task['test:c'].invoke
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
namespace :gem do
|
170
|
+
|
171
|
+
desc "Install the gem locally"
|
172
|
+
task :install do
|
173
|
+
`gem build bson.gemspec`
|
174
|
+
`gem install --no-rdoc --no-ri bson-*.gem`
|
175
|
+
|
176
|
+
`gem build mongo.gemspec`
|
177
|
+
`gem install --no-rdoc --no-ri mongo-*.gem`
|
178
|
+
|
179
|
+
`rm mongo-*.gem`
|
180
|
+
`rm bson-*.gem`
|
181
|
+
end
|
182
|
+
|
183
|
+
desc "Install the optional c extensions"
|
184
|
+
task :install_extensions do
|
185
|
+
`gem uninstall bson_ext`
|
186
|
+
`gem build bson_ext.gemspec`
|
187
|
+
`gem install --no-rdoc --no-ri bson_ext-*.gem`
|
188
|
+
`rm bson_ext-*.gem`
|
189
|
+
end
|
190
|
+
|
191
|
+
desc "Build all gems"
|
192
|
+
task :build_all do
|
193
|
+
`gem build mongo.gemspec`
|
194
|
+
`gem build bson.gemspec`
|
195
|
+
`gem build bson.java.gemspec`
|
196
|
+
`gem build bson_ext.gemspec`
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
namespace :ci do
|
202
|
+
namespace :test do
|
203
|
+
task :c do
|
204
|
+
Rake::Task['gem:install'].invoke
|
205
|
+
Rake::Task['gem:install_extensions'].invoke
|
206
|
+
Rake::Task['test:c'].invoke
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
task :default => :list
|
212
|
+
|
213
|
+
task :list do
|
214
|
+
system 'rake -T'
|
215
|
+
end
|
data/bin/mongo_console
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
org_argv = ARGV.dup
|
3
|
+
ARGV.clear
|
4
|
+
|
5
|
+
require 'irb'
|
6
|
+
|
7
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
8
|
+
require 'mongo'
|
9
|
+
|
10
|
+
include Mongo
|
11
|
+
|
12
|
+
host = org_argv[0] || ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
13
|
+
port = org_argv[1] || ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
14
|
+
dbnm = org_argv[2] || ENV['MONGO_RUBY_DRIVER_DB'] || 'ruby-mongo-console'
|
15
|
+
|
16
|
+
puts "Connecting to #{host}:#{port} (CONN) on with database #{dbnm} (DB)"
|
17
|
+
CONN = Connection.new(host, port)
|
18
|
+
DB = CONN.db(dbnm)
|
19
|
+
|
20
|
+
puts "Starting IRB session..."
|
21
|
+
IRB.start(__FILE__)
|
data/docs/CREDITS.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# Credits
|
2
|
+
|
3
|
+
Adrian Madrid, aemadrid@gmail.com
|
4
|
+
|
5
|
+
* bin/mongo_console
|
6
|
+
* examples/benchmarks.rb
|
7
|
+
* examples/irb.rb
|
8
|
+
* Modifications to examples/simple.rb
|
9
|
+
* Found plenty of bugs and missing features.
|
10
|
+
* Ruby 1.9 support.
|
11
|
+
* Gem support.
|
12
|
+
* Many other code suggestions and improvements.
|
13
|
+
|
14
|
+
Aman Gupta, aman@tmm1.net
|
15
|
+
|
16
|
+
* Collection#save
|
17
|
+
* Noted bug in returning query batch size.
|
18
|
+
|
19
|
+
Jon Crosby, jon@joncrosby.me
|
20
|
+
|
21
|
+
* Some code clean-up
|
22
|
+
|
23
|
+
John Nunemaker, http://railstips.org
|
24
|
+
|
25
|
+
* Collection#create_index takes symbols as well as strings
|
26
|
+
* Fix for Collection#save
|
27
|
+
* Add logger convenience methods to connection and database
|
28
|
+
|
29
|
+
David James, djames@sunlightfoundation.com
|
30
|
+
|
31
|
+
* Fix dates to return as UTC
|
32
|
+
|
33
|
+
Paul Dlug, paul.dlug@gmail.com
|
34
|
+
|
35
|
+
* Generate _id on the client side if not provided
|
36
|
+
* Collection#insert and Collection#save return _id
|
37
|
+
|
38
|
+
Durran Jordan, durran@gmail.com
|
39
|
+
|
40
|
+
* DB#collections
|
41
|
+
* Support for specifying sort order as array of [key, direction] pairs
|
42
|
+
* OrderedHash#update aliases to merge!
|
43
|
+
|
44
|
+
Cyril Mougel, cyril.mougel@gmail.com
|
45
|
+
|
46
|
+
* Initial logging support
|
47
|
+
* Test case
|
48
|
+
|
49
|
+
Jack Chen, chendo on github
|
50
|
+
|
51
|
+
* Test case + fix for deserializing pre-epoch Time instances
|
52
|
+
|
53
|
+
Michael Bernstein, mrb on github
|
54
|
+
|
55
|
+
* Cursor#sort
|
56
|
+
|
57
|
+
Paulo Ahahgon, pahagon on github
|
58
|
+
|
59
|
+
* removed hard limit
|
60
|
+
|
61
|
+
Les Hill, leshill on github
|
62
|
+
|
63
|
+
* OrderedHash#each returns self
|
64
|
+
|
65
|
+
Sean Cribbs, seancribbs on github
|
66
|
+
|
67
|
+
* Modified standard_benchmark to allow profiling
|
68
|
+
* C ext for faster ObjectID creation
|
69
|
+
|
70
|
+
Sunny Hirai
|
71
|
+
|
72
|
+
* Suggested hashcode fix for Mongo::ObjectID
|
73
|
+
* Noted index ordering bug.
|
74
|
+
* GridFS performance boost
|
75
|
+
|
76
|
+
Christos Trochalakis
|
77
|
+
|
78
|
+
* Added map/reduce helper
|
79
|
+
|
80
|
+
Blythe Dunham
|
81
|
+
|
82
|
+
* Added finalize option to map/reduce
|
83
|
+
|
84
|
+
Matt Powell (fauxparse)
|
85
|
+
|
86
|
+
* Added GridStore#mv
|
87
|
+
|
88
|
+
Patrick Collison
|
89
|
+
|
90
|
+
* Added safe mode for Collection#remove
|
91
|
+
|
92
|
+
Chuck Remes
|
93
|
+
|
94
|
+
* Extraction of BSON into separate gems
|
95
|
+
* Extensions compile on Rubinius
|
96
|
+
* Performance improvements for INT in C extensions
|
97
|
+
* Performance improvements for JRuby BSON encoder and callback classes
|
98
|
+
|
99
|
+
Dmitrii Golub (Houdini) and Jacques Crocker (railsjedi)
|
100
|
+
|
101
|
+
* Support open to exclude fields on query
|
102
|
+
|
103
|
+
dfitzgibbon
|
104
|
+
|
105
|
+
* patch for ensuring bson_ext compatibility with early release of Ruby 1.8.5
|
106
|
+
|
107
|
+
Matt Taylor
|
108
|
+
|
109
|
+
* Noticed excessive calls to ObjectId#to_s. As a result, stopped creating
|
110
|
+
log messages when no logger was passed to Mongo::Connection. Resulted in a significant
|
111
|
+
performance improvement.
|
112
|
+
|
113
|
+
Hongli Lai (Phusion)
|
114
|
+
|
115
|
+
* Significant performance improvements. See commits.
|
116
|
+
|
117
|
+
Mislav Marohnić
|
118
|
+
|
119
|
+
* Replaced returning with each_with_object
|
120
|
+
|
121
|
+
Alex Stupka
|
122
|
+
|
123
|
+
* Replica set port bug
|
data/docs/FAQ.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# Ruby MongoDB FAQ
|
2
|
+
|
3
|
+
This is a list of frequently asked questions about using Ruby with MongoDB. If you have a question you'd like to have answered here, please post your question to the [mongodb-user list](http://groups.google.com/group/mongodb-user).
|
4
|
+
|
5
|
+
#### Can I run (insert command name here) from the Ruby driver?
|
6
|
+
|
7
|
+
Yes. You can run any of the [available database commands|List of Database Commands] from the driver using the DB#command method. The only trick is to use an OrderedHash when specifying the command. For example, here's how you'd run an asynchronous fsync from the driver:
|
8
|
+
|
9
|
+
|
10
|
+
# This command is run on the admin database.
|
11
|
+
@db = Mongo::Connection.new.db('admin')
|
12
|
+
|
13
|
+
# Build the command.
|
14
|
+
cmd = OrderedHash.new
|
15
|
+
cmd['fsync'] = 1
|
16
|
+
cmd['async'] = true
|
17
|
+
|
18
|
+
# Run it.
|
19
|
+
@db.command(cmd)
|
20
|
+
|
21
|
+
|
22
|
+
It's important to keep in mind that some commands, like `fsync`, must be run on the `admin` database, while other commands can be run on any database. If you're having trouble, check the [command reference|List of Database Commands] to make sure you're using the command correctly.
|
23
|
+
|
24
|
+
#### Does the Ruby driver support an EXPLAIN command?
|
25
|
+
|
26
|
+
Yes. `explain` is, technically speaking, an option sent to a query that tells MongoDB to return an explain plan rather than the query's results. You can use `explain` by constructing a query and calling explain at the end:
|
27
|
+
|
28
|
+
|
29
|
+
@collection = @db['users']
|
30
|
+
result = @collection.find({:name => "jones"}).explain
|
31
|
+
|
32
|
+
|
33
|
+
The resulting explain plan might look something like this:
|
34
|
+
|
35
|
+
|
36
|
+
{"cursor"=>"BtreeCursor name_1",
|
37
|
+
"startKey"=>{"name"=>"Jones"},
|
38
|
+
"endKey"=>{"name"=>"Jones"},
|
39
|
+
"nscanned"=>1.0,
|
40
|
+
"n"=>1,
|
41
|
+
"millis"=>0,
|
42
|
+
"oldPlan"=>{"cursor"=>"BtreeCursor name_1",
|
43
|
+
"startKey"=>{"name"=>"Jones"},
|
44
|
+
"endKey"=>{"name"=>"Jones"}
|
45
|
+
},
|
46
|
+
"allPlans"=>[{"cursor"=>"BtreeCursor name_1",
|
47
|
+
"startKey"=>{"name"=>"Jones"},
|
48
|
+
"endKey"=>{"name"=>"Jones"`]
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
Because this collection has an index on the "name" field, the query uses that index, only having to scan a single record. "n" is the number of records the query will return. "millis" is the time the query takes, in milliseconds. "oldPlan" indicates that the query optimizer has already seen this kind of query and has, therefore, saved an efficient query plan. "allPlans" shows all the plans considered for this query.
|
53
|
+
|
54
|
+
#### I see that BSON supports a symbol type. Does this mean that I can store Ruby symbols in MongoDB?
|
55
|
+
|
56
|
+
You can store Ruby symbols in MongoDB, but only as values. BSON specifies that document keys must be strings. So, for instance, you can do this:
|
57
|
+
|
58
|
+
|
59
|
+
@collection = @db['test']
|
60
|
+
|
61
|
+
boat_id = @collection.save({:vehicle => :boat})
|
62
|
+
car_id = @collection.save({"vehicle" => "car"})
|
63
|
+
|
64
|
+
@collection.find_one('_id' => boat_id)
|
65
|
+
{"_id" => ObjectID('4bb372a8238d3b5c8c000001'), "vehicle" => :boat}
|
66
|
+
|
67
|
+
|
68
|
+
@collection.find_one('_id' => car_id)
|
69
|
+
{"_id" => ObjectID('4bb372a8238d3b5c8c000002'), "vehicle" => "car"}
|
70
|
+
|
71
|
+
|
72
|
+
Notice that the symbol values are returned as expected, but that symbol keys are treated as strings.
|
73
|
+
|
74
|
+
#### Why can't I access random elements within a cursor?
|
75
|
+
|
76
|
+
MongoDB cursors are designed for sequentially iterating over a result set, and all the drivers, including the Ruby driver, stick closely to this directive. Internally, a Ruby cursor fetches results in batches by running a MongoDB `getmore` operation. The results are buffered for efficient iteration on the client-side.
|
77
|
+
|
78
|
+
What this means is that a cursor is nothing more than a device for returning a result set on a query that's been initiated on the server. Cursors are not containers for result sets. If we allow a cursor to be randomly accessed, then we run into issues regarding the freshness of the data. For instance, if I iterate over a cursor and then want to retrieve the cursor's first element, should a stored copy be returned, or should the cursor re-run the query? If we returned a stored copy, it may not be fresh. And if the the query is re-run, then we're technically dealing with a new cursor.
|
79
|
+
|
80
|
+
To avoid those issues, we're saying that anyone who needs flexible access to the results of a query should store those results in an array and then access the data as needed.
|
81
|
+
|
82
|
+
#### Why can't I save an instance of TimeWithZone?
|
83
|
+
|
84
|
+
MongoDB stores times in UTC as the number of milliseconds since the epoch. This means that the Ruby driver serializes Ruby Time objects only. While it would certainly be possible to serialize a TimeWithZone, this isn't preferable since the driver would still deserialize to a Time object.
|
85
|
+
|
86
|
+
All that said, if necessary, it'd be easy to write a thin wrapper over the driver that would store an extra time zone attribute and handle the serialization/deserialization of TimeWithZone transparently.
|
87
|
+
|
88
|
+
#### I keep getting CURSOR_NOT_FOUND exceptions. What's happening?
|
89
|
+
|
90
|
+
The most likely culprit here is that the cursor is timing out on the server. Whenever you issue a query, a cursor is created on the server. Cursor naturally time out after ten minutes, which means that if you happen to be iterating over a cursor for more than ten minutes, you risk a CURSOR_NOT_FOUND exception.
|
91
|
+
|
92
|
+
There are two solutions to this problem. You can either:
|
93
|
+
|
94
|
+
1. Limit your query. Use some combination of `limit` and `skip` to reduce the total number of query results. This will, obviously, bring down the time it takes to iterate.
|
95
|
+
|
96
|
+
2. Turn off the cursor timeout. To do that, invoke `find` with a block, and pass `:timeout => true`:
|
97
|
+
|
98
|
+
@collection.find({}, :timeout => false) do |cursor|
|
99
|
+
cursor.each do |document
|
100
|
+
# Process documents here
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
#### I periodically see connection failures between the driver and MongoDB. Why can't the driver retry the operation automatically?
|
105
|
+
|
106
|
+
A connection failure can indicate any number of failure scenarios. Has the server crashed? Are we experiencing a temporary network partition? Is there a bug in our ssh tunnel?
|
107
|
+
|
108
|
+
Without further investigation, it's impossible to know exactly what has caused the connection failure. Furthermore, when we do see a connection failure, it's impossible to know how many operations prior to the failure succeeded. Imagine, for instance, that we're using safe mode and we send an `$inc` operation to the server. It's entirely possible that the server has received the `$inc` but failed on the call to `getLastError`. In that case, retrying the operation would result in a double-increment.
|
109
|
+
|
110
|
+
Because of the indeterminacy involved, the MongoDB drivers will not retry operations on connection failure. How connection failures should be handled is entirely dependent on the application. Therefore, we leave it to the application developers to make the best decision in this case.
|
111
|
+
|
112
|
+
The drivers will reconnect on the subsequent operation.
|
113
|
+
|
114
|
+
#### I ocassionally get an error saying that responses are out of order. What's happening?
|
115
|
+
|
116
|
+
See (this JIRA issue)[http://jira.mongodb.org/browse/RUBY-221].
|