diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 0399d45..5cbaae2 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -139,6 +139,48 @@ module XGen @db.drop_collection(@name) end + # Perform a query similar to an SQL group by operation. + # + # Returns an array of grouped items. + # + # :keys :: list of fields to group by + # :condition :: specification of rows to be considered (as a 'find' + # query specification) + # :initial :: initial value of the aggregation counter object + # :reduce :: aggregation function as a JavaScript string + def group(keys, condition, initial, reduce) + group_function = < @name, + "keys" => keys, + "condition" => condition, + "initial" => initial + }))["result"] + end + # Return an array of hashes, one for each index. Each hash contains: # # :name :: Index name diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 3d0a324..5675514 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -530,6 +530,20 @@ class DBAPITest < Test::Unit::TestCase assert id != 0 end + def test_group + @@db.drop_collection("test") + test = @@db.collection("test") + + assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }") + + test.insert("a" => 2) + test.insert("b" => 5) + test.insert("a" => 1) + + assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"] + assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"] + end + # TODO this test fails with error message "Undefed Before end of object" # That is a database error. The undefined type may go away.