Added support for map-reduce

This commit is contained in:
Christos Trochalakis 2009-11-23 10:04:48 +02:00 committed by Kyle Banker
parent 36423240a6
commit b1b61d5c0b
2 changed files with 39 additions and 0 deletions

View File

@ -298,6 +298,33 @@ module Mongo
@db.drop_collection(@name) @db.drop_collection(@name)
end end
def mapreduce(map, reduce, options={})
case map
when Code
else
map = Code.new(map)
end
case reduce
when Code
else
reduce = Code.new(reduce)
end
hash = OrderedHash.new
hash['mapreduce'] = self.name
hash['map'] = map
hash['reduce'] = reduce
hash.merge! options
result = @db.db_command(hash)
if result["ok"] == 1
return @db[result["result"]]
else
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end
end
# Perform a query similar to an SQL group by operation. # Perform a query similar to an SQL group by operation.
# #
# Returns an array of grouped items. # Returns an array of grouped items.

View File

@ -265,6 +265,18 @@ class TestCollection < Test::Unit::TestCase
assert c.closed? assert c.closed?
end end
def test_mapreduce
@@test << { "user_id" => 1 }
@@test << { "user_id" => 2 }
m = "function() { emit(this.user_id, 1); }"
r = "function(k,vals) { return 1; }"
res = @@test.mapreduce(m, r);
assert res.find_one({"_id" => 1})
assert res.find_one({"_id" => 2})
end
def test_saving_dates_pre_epoch def test_saving_dates_pre_epoch
begin begin
@@test.save({'date' => Time.utc(1600)}) @@test.save({'date' => Time.utc(1600)})