minor: Docs, cleanup, history.

This commit is contained in:
Kyle Banker 2009-11-25 11:25:50 -05:00
parent b1b61d5c0b
commit a3cbacc99b
4 changed files with 54 additions and 26 deletions

10
HISTORY
View File

@ -1,3 +1,13 @@
0.18 2009-11-25
* Connections now support connection pooling. See http://api.mongodb.org/ruby/0.18/classes/Mongo/Connection.html#M000158
* Deprecated :auto_reconnect option on connection; if the driver fails to
connect, it will automatically try to reconnect on the subsequent operation.
See http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby
* Added Collection#map_reduce helper (Christos Trochalakis)
* Deprecated DB#db_command in favor of DB#command.
* Removed deprecated old sort options, :offset, and Connection#clear.
* Lots of internal code restructuring for better maintainability.
0.17.1 2009-11-17 0.17.1 2009-11-17
* Index ordering fix * Index ordering fix
* Notice to install mongo_ext * Notice to install mongo_ext

View File

@ -376,6 +376,9 @@ Sunny Hirai
* Suggested hashcode fix for Mongo::ObjectID * Suggested hashcode fix for Mongo::ObjectID
* Noted index ordering bug. * Noted index ordering bug.
Christos Trochalakis
* Added map/reduce helper
= License = License
Copyright 2008-2009 10gen Inc. Copyright 2008-2009 10gen Inc.

View File

@ -298,18 +298,28 @@ module Mongo
@db.drop_collection(@name) @db.drop_collection(@name)
end end
def mapreduce(map, reduce, options={}) # Performs a map/reduce operation on the current collection. Returns a new
case map # collection containing the results of the operation.
when Code #
else # Required:
map = Code.new(map) # +map+ :: a map function, written in javascript.
end # +reduce+ :: a reduce function, written in javascript.
#
case reduce # Optional:
when Code # :query :: a query selector document, like what's passed to #find, to limit
else # the operation to a subset of the collection.
reduce = Code.new(reduce) # :sort :: sort parameters passed to the query.
end # :limit :: number of objects to return from the collection.
# :finalize :: a javascript function to apply to the result set after the
# map/reduce operation has finished.
# :out :: the name of the output collection. if specified, the collection will not be treated as temporary.
# :keeptemp :: if true, the generated collection will be persisted. default is false.
# :verbose :: if true, provides statistics on job execution time.
#
# For more information on using map/reduce, see http://www.mongodb.org/display/DOCS/MapReduce
def map_reduce(map, reduce, options={})
map = Code.new(map) unless map.is_a?(Code)
reduce = Code.new(reduce) unless reduce.is_a?(Code)
hash = OrderedHash.new hash = OrderedHash.new
hash['mapreduce'] = self.name hash['mapreduce'] = self.name
@ -317,16 +327,15 @@ module Mongo
hash['reduce'] = reduce hash['reduce'] = reduce
hash.merge! options hash.merge! options
result = @db.db_command(hash) result = @db.command(hash)
if result["ok"] == 1 unless result["ok"] == 1
return @db[result["result"]] raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
else
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end end
@db[result["result"]]
end end
alias :mapreduce :map_reduce
# Perform a query similar to an SQL group by operation. # Performs a group query, similar to the 'SQL GROUP BY' operation.
#
# Returns an array of grouped items. # Returns an array of grouped items.
# #
# :keys :: Array of fields to group by # :keys :: Array of fields to group by
@ -344,11 +353,7 @@ module Mongo
hash[k] = 1 hash[k] = 1
end end
case reduce reduce = Code.new(reduce) unless reduce.is_a?(Code)
when Code
else
reduce = Code.new(reduce)
end
result = @db.command({"group" => result = @db.command({"group" =>
{ {
@ -401,7 +406,7 @@ function () {
return {"result": map.values()}; return {"result": map.values()};
} }
EOS EOS
return @db.eval(Code.new(group_function, scope))["result"] @db.eval(Code.new(group_function, scope))["result"]
end end
# Returns a list of distinct values for +key+ across all # Returns a list of distinct values for +key+ across all

View File

@ -271,10 +271,20 @@ class TestCollection < Test::Unit::TestCase
m = "function() { emit(this.user_id, 1); }" m = "function() { emit(this.user_id, 1); }"
r = "function(k,vals) { return 1; }" r = "function(k,vals) { return 1; }"
res = @@test.mapreduce(m, r); res = @@test.map_reduce(m, r);
assert res.find_one({"_id" => 1}) assert res.find_one({"_id" => 1})
assert res.find_one({"_id" => 2}) assert res.find_one({"_id" => 2})
end
def test_mapreduce_with_code_objects
@@test << { "user_id" => 1 }
@@test << { "user_id" => 2 }
m = Code.new("function() { emit(this.user_id, 1); }")
r = Code.new("function(k,vals) { return 1; }")
res = @@test.map_reduce(m, r);
assert res.find_one({"_id" => 1})
assert res.find_one({"_id" => 2})
end end
def test_saving_dates_pre_epoch def test_saving_dates_pre_epoch