mongodb-mongo 0.6.6 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +0 -4
- data/Rakefile +1 -1
- data/lib/mongo/collection.rb +42 -0
- data/lib/mongo/db.rb +5 -0
- data/lib/mongo/util/bson.rb +7 -1
- data/mongo-ruby-driver.gemspec +1 -1
- data/tests/test_db_api.rb +36 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -288,10 +288,6 @@ See the git log comments.
|
|
288
288
|
|
289
289
|
= To Do
|
290
290
|
|
291
|
-
* Add group_by. Need to figure out how we are going to send functions. The
|
292
|
-
current thinking is that Mongo will allow a subset of JavaScript (which we
|
293
|
-
would have to send as a string), but this is still under discussion.
|
294
|
-
|
295
291
|
* Tests for update and repsert.
|
296
292
|
|
297
293
|
* Add a way to specify a collection of databases on startup (a simple array of
|
data/Rakefile
CHANGED
@@ -23,7 +23,7 @@ desc "Generate documentation"
|
|
23
23
|
task :rdoc do
|
24
24
|
version = eval(File.read("mongo-ruby-driver.gemspec")).version
|
25
25
|
out = File.join('html', version.to_s)
|
26
|
-
FileUtils.rm_rf(
|
26
|
+
FileUtils.rm_rf('html')
|
27
27
|
system "rdoc --main README.rdoc --op #{out} --inline-source --quiet README.rdoc `find lib -name '*.rb'`"
|
28
28
|
end
|
29
29
|
|
data/lib/mongo/collection.rb
CHANGED
@@ -139,6 +139,48 @@ module XGen
|
|
139
139
|
@db.drop_collection(@name)
|
140
140
|
end
|
141
141
|
|
142
|
+
# Perform a query similar to an SQL group by operation.
|
143
|
+
#
|
144
|
+
# Returns an array of grouped items.
|
145
|
+
#
|
146
|
+
# :keys :: list of fields to group by
|
147
|
+
# :condition :: specification of rows to be considered (as a 'find'
|
148
|
+
# query specification)
|
149
|
+
# :initial :: initial value of the aggregation counter object
|
150
|
+
# :reduce :: aggregation function as a JavaScript string
|
151
|
+
def group(keys, condition, initial, reduce)
|
152
|
+
group_function = <<EOS
|
153
|
+
function () {
|
154
|
+
var c = db[ns].find(condition);
|
155
|
+
var map = new Map();
|
156
|
+
var reduce_function = #{reduce};
|
157
|
+
while (c.hasNext()) {
|
158
|
+
var obj = c.next();
|
159
|
+
|
160
|
+
var key = {};
|
161
|
+
for (var i in keys) {
|
162
|
+
key[keys[i]] = obj[keys[i]];
|
163
|
+
}
|
164
|
+
|
165
|
+
var aggObj = map[key];
|
166
|
+
if (aggObj == null) {
|
167
|
+
var newObj = Object.extend({}, key);
|
168
|
+
aggObj = map[key] = Object.extend(newObj, initial);
|
169
|
+
}
|
170
|
+
reduce_function(obj, aggObj);
|
171
|
+
}
|
172
|
+
return {"result": map.values()};
|
173
|
+
}
|
174
|
+
EOS
|
175
|
+
return @db.eval(Code.new(group_function,
|
176
|
+
{
|
177
|
+
"ns" => @name,
|
178
|
+
"keys" => keys,
|
179
|
+
"condition" => condition,
|
180
|
+
"initial" => initial
|
181
|
+
}))["result"]
|
182
|
+
end
|
183
|
+
|
142
184
|
# Return an array of hashes, one for each index. Each hash contains:
|
143
185
|
#
|
144
186
|
# :name :: Index name
|
data/lib/mongo/db.rb
CHANGED
@@ -372,6 +372,11 @@ module XGen
|
|
372
372
|
raise "Error with count command: #{doc.inspect}"
|
373
373
|
end
|
374
374
|
|
375
|
+
# Dereference a DBRef, getting the document it points to.
|
376
|
+
def dereference(dbref)
|
377
|
+
collection(dbref.namespace).find_first("_id" => dbref.object_id)
|
378
|
+
end
|
379
|
+
|
375
380
|
# Evaluate a JavaScript expression on MongoDB.
|
376
381
|
# +code+ should be a string or Code instance containing a JavaScript
|
377
382
|
# expression. Additional arguments will be passed to that expression
|
data/lib/mongo/util/bson.rb
CHANGED
@@ -85,7 +85,13 @@ class BSON
|
|
85
85
|
@buf.put_int(0)
|
86
86
|
|
87
87
|
# Write key/value pairs. Always write _id first if it exists.
|
88
|
-
|
88
|
+
if obj.has_key? '_id'
|
89
|
+
oid = obj['_id']
|
90
|
+
elsif obj.has_key? :_id
|
91
|
+
oid = obj[:_id]
|
92
|
+
else
|
93
|
+
oid = false
|
94
|
+
end
|
89
95
|
serialize_key_value('_id', oid) if oid
|
90
96
|
obj.each {|k, v| serialize_key_value(k, v) unless k == '_id' || k == :_id }
|
91
97
|
|
data/mongo-ruby-driver.gemspec
CHANGED
@@ -79,7 +79,7 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
|
|
79
79
|
|
80
80
|
Gem::Specification.new do |s|
|
81
81
|
s.name = 'mongo'
|
82
|
-
s.version = '0.6.
|
82
|
+
s.version = '0.6.7'
|
83
83
|
s.platform = Gem::Platform::RUBY
|
84
84
|
s.summary = 'Ruby driver for the 10gen Mongo DB'
|
85
85
|
s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
|
data/tests/test_db_api.rb
CHANGED
@@ -522,6 +522,42 @@ class DBAPITest < Test::Unit::TestCase
|
|
522
522
|
end
|
523
523
|
end
|
524
524
|
|
525
|
+
def test_hash_default_value_id
|
526
|
+
val = Hash.new(0)
|
527
|
+
val["x"] = 5
|
528
|
+
@@coll.insert val
|
529
|
+
id = @@coll.find_first("x" => 5)["_id"]
|
530
|
+
assert id != 0
|
531
|
+
end
|
532
|
+
|
533
|
+
def test_group
|
534
|
+
@@db.drop_collection("test")
|
535
|
+
test = @@db.collection("test")
|
536
|
+
|
537
|
+
assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
|
538
|
+
|
539
|
+
test.insert("a" => 2)
|
540
|
+
test.insert("b" => 5)
|
541
|
+
test.insert("a" => 1)
|
542
|
+
|
543
|
+
assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
544
|
+
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
545
|
+
end
|
546
|
+
|
547
|
+
def test_deref
|
548
|
+
@@coll.clear
|
549
|
+
|
550
|
+
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
|
551
|
+
obj = {"x" => true}
|
552
|
+
key = @@coll.insert(obj)["_id"]
|
553
|
+
assert_equal true, @@db.dereference(DBRef.new("test", key))["x"]
|
554
|
+
|
555
|
+
assert_equal nil, @@db.dereference(DBRef.new("test", 4))
|
556
|
+
obj = {"_id" => 4}
|
557
|
+
@@coll.insert(obj)
|
558
|
+
assert_equal obj, @@db.dereference(DBRef.new("test", 4))
|
559
|
+
end
|
560
|
+
|
525
561
|
# TODO this test fails with error message "Undefed Before end of object"
|
526
562
|
# That is a database error. The undefined type may go away.
|
527
563
|
|