mongodb-mongo 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +64 -0
- data/mongo-ruby-driver.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -25,6 +25,10 @@ A quick code sample:
|
|
25
25
|
puts "There are #{coll.count()} records. Here they are:"
|
26
26
|
coll.find().each { |doc| puts doc.inspect }
|
27
27
|
|
28
|
+
This driver also includes an implementation of a GridStore class, a Ruby
|
29
|
+
interface to Mongo's GridFS storage. NOTE: the GridStore code may be moved to
|
30
|
+
a separate project.
|
31
|
+
|
28
32
|
= Installation
|
29
33
|
|
30
34
|
Install the "mongo" gem by typing
|
@@ -54,6 +58,52 @@ be running, of course.
|
|
54
58
|
|
55
59
|
See also the test code, especially tests/test_db_api.rb.
|
56
60
|
|
61
|
+
For the GridFS class GridStore, see the tests.
|
62
|
+
|
63
|
+
|
64
|
+
= The Driver
|
65
|
+
|
66
|
+
Here is some simple example code:
|
67
|
+
|
68
|
+
require 'rubygems' # not required for Ruby 1.9
|
69
|
+
require 'mongo'
|
70
|
+
|
71
|
+
include XGen::Mongo::Driver
|
72
|
+
db = Mongo.new.db('my-db-name')
|
73
|
+
things = db.collection('things')
|
74
|
+
|
75
|
+
things.clear
|
76
|
+
things.insert('a' => 42)
|
77
|
+
things.insert('a' => 99, 'b' => Time.now)
|
78
|
+
puts things.count # => 2
|
79
|
+
puts things.find('a' => 42).next_object.inspect # {"a"=>42}
|
80
|
+
|
81
|
+
|
82
|
+
= GridStore
|
83
|
+
|
84
|
+
The GridStore class is a Ruby implementation of Mongo's GridFS file storage
|
85
|
+
system. An instance of GridStore is like an IO object. See the rdocs for
|
86
|
+
details.
|
87
|
+
|
88
|
+
Note that the GridStore class is not automatically required when you require
|
89
|
+
'mongo'. You need to require 'mongo/gridfs'.
|
90
|
+
|
91
|
+
Example code:
|
92
|
+
|
93
|
+
GridStore.open(database, 'filename', 'w') { |f|
|
94
|
+
f.puts "Hello, world!"
|
95
|
+
}
|
96
|
+
GridStore.open(database, 'filename, 'r') { |f|
|
97
|
+
puts f.read # => Hello, world!\n
|
98
|
+
}
|
99
|
+
GridStore.open(database, 'filename', 'w+') { |f|
|
100
|
+
f.puts "But wait, there's more!"
|
101
|
+
}
|
102
|
+
GridStore.open(database, 'filename, 'r') { |f|
|
103
|
+
puts f.read # => Hello, world!\nBut wait, there's more!\n
|
104
|
+
}
|
105
|
+
|
106
|
+
|
57
107
|
|
58
108
|
= Notes
|
59
109
|
|
@@ -99,6 +149,20 @@ Here is a sample primary key factory, taken from the tests:
|
|
99
149
|
end
|
100
150
|
end
|
101
151
|
|
152
|
+
Here's a slightly more sophisticated one that handles both symbol and string
|
153
|
+
keys. This is the PKFactory that comes with the MongoRecord code (an
|
154
|
+
ActiveRecord-like framework for non-Rails apps) and the AR Mongo adapter code
|
155
|
+
(for Rails):
|
156
|
+
|
157
|
+
class PKFactory
|
158
|
+
def create_pk(row)
|
159
|
+
return row if row[:_id]
|
160
|
+
row.delete(:_id) # in case it exists but the value is nil
|
161
|
+
row['_id'] ||= XGen::Mongo::Driver::ObjectID.new
|
162
|
+
row
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
102
166
|
A database's PK factory object may be set after you obtain it, but only once.
|
103
167
|
The only reason it is changeable is so that libraries such as MongoRecord that
|
104
168
|
use this driver can set the PK factory after obtaining the database but before
|
data/mongo-ruby-driver.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongo'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.4.0'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
|
6
6
|
s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
|