mongo 1.1.1 → 1.1.2
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 +1 -13
- data/{README.rdoc → README.md} +113 -151
- data/Rakefile +17 -4
- data/docs/1.0_UPGRADE.md +21 -0
- data/docs/CREDITS.md +119 -0
- data/docs/HISTORY.md +158 -0
- data/docs/TUTORIAL.md +247 -0
- data/lib/mongo.rb +1 -1
- data/lib/mongo/collection.rb +67 -32
- data/lib/mongo/connection.rb +39 -8
- data/lib/mongo/cursor.rb +1 -1
- data/lib/mongo/db.rb +19 -6
- data/lib/mongo/exceptions.rb +3 -0
- data/lib/mongo/gridfs/grid.rb +4 -1
- data/lib/mongo/gridfs/grid_file_system.rb +5 -2
- data/lib/mongo/util/pool.rb +115 -0
- data/mongo.gemspec +4 -5
- data/test/bson/bson_test.rb +3 -1
- data/test/collection_test.rb +30 -0
- data/test/connection_test.rb +65 -52
- data/test/cursor_test.rb +27 -0
- data/test/replica_sets/connect_test.rb +24 -1
- data/test/safe_test.rb +42 -0
- data/test/unit/db_test.rb +2 -0
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/safe_test.rb +125 -0
- metadata +18 -22
- data/HISTORY +0 -215
- data/bin/bson_benchmark.rb +0 -59
- data/bin/fail_if_no_c.rb +0 -11
- data/bin/insert.rb +0 -35
- data/bin/oid.rb +0 -13
- data/examples/admin.rb +0 -43
- data/examples/capped.rb +0 -22
- data/examples/cursor.rb +0 -48
- data/examples/gridfs.rb +0 -44
- data/examples/index_test.rb +0 -126
- data/examples/info.rb +0 -31
- data/examples/queries.rb +0 -70
- data/examples/simple.rb +0 -24
- data/examples/strict.rb +0 -35
- data/examples/types.rb +0 -36
data/bin/bson_benchmark.rb
DELETED
@@ -1,59 +0,0 @@
|
|
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 = Mongo::BSON_CODER.new
|
13
|
-
TRIALS.times { |i|
|
14
|
-
b = Mongo::BSON_CODER.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
DELETED
data/bin/insert.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'mongo'
|
5
|
-
require 'date'
|
6
|
-
require 'logger'
|
7
|
-
include Mongo
|
8
|
-
|
9
|
-
@logger = Logger.new(File.open("m.log", "w"))
|
10
|
-
require 'ruby-prof'
|
11
|
-
|
12
|
-
num_inserts = 100000
|
13
|
-
if( ARGV.size() > 0 ) then
|
14
|
-
num_inserts = ARGV[0].to_i()
|
15
|
-
end
|
16
|
-
db = Connection.new('localhost', 27017).db('sample-db')
|
17
|
-
coll = db.collection('test')
|
18
|
-
coll.remove()
|
19
|
-
sleep(2)
|
20
|
-
|
21
|
-
puts "Testing #{num_inserts} inserts"
|
22
|
-
start = Time.now()
|
23
|
-
|
24
|
-
#RubyProf.start
|
25
|
-
num_inserts.times do |i|
|
26
|
-
coll.insert({'a' => i+1})
|
27
|
-
end
|
28
|
-
#result = RubyProf.stop
|
29
|
-
ending = Time.now
|
30
|
-
total = ending - start
|
31
|
-
|
32
|
-
puts "Took #{total} seconds, meaning #{num_inserts / total} per second."
|
33
|
-
|
34
|
-
#printer = RubyProf::FlatPrinter.new(result)
|
35
|
-
#printer.print(STDOUT, 0)
|
data/bin/oid.rb
DELETED
data/examples/admin.rb
DELETED
@@ -1,43 +0,0 @@
|
|
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
|
-
con = Mongo::Connection.new(host, port)
|
13
|
-
db = con.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 = con['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
DELETED
@@ -1,22 +0,0 @@
|
|
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
DELETED
@@ -1,48 +0,0 @@
|
|
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
DELETED
@@ -1,44 +0,0 @@
|
|
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'] || Connection::DEFAULT_PORT
|
11
|
-
|
12
|
-
puts "Connecting to #{host}:#{port}"
|
13
|
-
db = Connection.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
|
data/examples/index_test.rb
DELETED
@@ -1,126 +0,0 @@
|
|
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
DELETED
@@ -1,31 +0,0 @@
|
|
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
|
-
coll.create_index('a')
|
28
|
-
p db.index_information('test')
|
29
|
-
|
30
|
-
# Destroy the collection
|
31
|
-
coll.drop
|
data/examples/queries.rb
DELETED
@@ -1,70 +0,0 @@
|
|
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')
|