Permalink
Browse files

minor: Docs, cleanup, history.

  • Loading branch information...
1 parent b1b61d5 commit a3cbacc99b3c0592e8ba864398a32282bbbcba18 @banker banker committed Nov 25, 2009
Showing with 54 additions and 26 deletions.
  1. +10 −0 HISTORY
  2. +3 −0 README.rdoc
  3. +30 −25 lib/mongo/collection.rb
  4. +11 −1 test/test_collection.rb
View
10 HISTORY
@@ -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
* Index ordering fix
* Notice to install mongo_ext
View
@@ -376,6 +376,9 @@ Sunny Hirai
* Suggested hashcode fix for Mongo::ObjectID
* Noted index ordering bug.
+Christos Trochalakis
+* Added map/reduce helper
+
= License
Copyright 2008-2009 10gen Inc.
View
@@ -298,35 +298,44 @@ def drop
@db.drop_collection(@name)
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
+ # Performs a map/reduce operation on the current collection. Returns a new
+ # collection containing the results of the operation.
+ #
+ # Required:
+ # +map+ :: a map function, written in javascript.
+ # +reduce+ :: a reduce function, written in javascript.
+ #
+ # Optional:
+ # :query :: a query selector document, like what's passed to #find, to limit
+ # the operation to a subset of the collection.
+ # :sort :: sort parameters passed to the query.
+ # :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['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']}"
+ result = @db.command(hash)
+ unless result["ok"] == 1
+ raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end
+ @db[result["result"]]
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.
#
# :keys :: Array of fields to group by
@@ -344,11 +353,7 @@ def group(keys, condition, initial, reduce, command=false)
hash[k] = 1
end
- case reduce
- when Code
- else
- reduce = Code.new(reduce)
- end
+ reduce = Code.new(reduce) unless reduce.is_a?(Code)
result = @db.command({"group" =>
{
@@ -401,7 +406,7 @@ def group(keys, condition, initial, reduce, command=false)
return {"result": map.values()};
}
EOS
- return @db.eval(Code.new(group_function, scope))["result"]
+ @db.eval(Code.new(group_function, scope))["result"]
end
# Returns a list of distinct values for +key+ across all
View
@@ -271,10 +271,20 @@ def test_mapreduce
m = "function() { emit(this.user_id, 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" => 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
def test_saving_dates_pre_epoch

0 comments on commit a3cbacc

Please sign in to comment.