Added Collection#distinct [RUBY-61]
This commit is contained in:
parent
f47db82767
commit
5a81cb70ee
|
@ -383,6 +383,28 @@ EOS
|
|||
return @db.eval(Code.new(group_function, scope))["result"]
|
||||
end
|
||||
|
||||
# Returns a list of distinct values for +key+ across all
|
||||
# documents in the collection. The key may use dot notation
|
||||
# to reach into an embedded object.
|
||||
# @collection.save({:zip => 10010, :name => {:age => 27}})
|
||||
# @collection.save({:zip => 94108, :name => {:age => 24}})
|
||||
# @collection.save({:zip => 10010, :name => {:age => 27}})
|
||||
# @collection.save({:zip => 99701, :name => {:age => 24}})
|
||||
# @collection.save({:zip => 94108, :name => {:age => 27}})
|
||||
#
|
||||
# @collection.distinct(:zip)
|
||||
# [10010, 94108, 99701]
|
||||
# @collection.distinct("name.age")
|
||||
# [27, 24]
|
||||
def distinct(key)
|
||||
raise MongoArgumentError unless [String, Symbol].include?(key.class)
|
||||
command = OrderedHash.new
|
||||
command[:distinct] = @name
|
||||
command[:key] = key.to_s
|
||||
|
||||
@db.db_command(command)["values"]
|
||||
end
|
||||
|
||||
# Rename this collection.
|
||||
#
|
||||
# If operating in auth mode, client must be authorized as an admin to
|
||||
|
|
|
@ -21,6 +21,9 @@ module Mongo
|
|||
# Raised when configuration options cause connections, queries, etc., to fail.
|
||||
class ConfigurationError < MongoRubyError; end
|
||||
|
||||
# Raised when invalid arguments are sent to Mongo Ruby methods.
|
||||
class MongoArgumentError < MongoRubyError; end
|
||||
|
||||
# Raised when a database operation fails.
|
||||
class OperationFailure < RuntimeError; end
|
||||
|
||||
|
|
|
@ -59,6 +59,21 @@ class TestCollection < Test::Unit::TestCase
|
|||
assert_equal 5, @@db.collection("test.foo").find_one()["x"]
|
||||
end
|
||||
|
||||
if @@version > "1.1"
|
||||
def test_distinct
|
||||
@@test.remove
|
||||
@@test.insert([{:a => 0, :b => {:c => "a"}},
|
||||
{:a => 1, :b => {:c => "b"}},
|
||||
{:a => 1, :b => {:c => "c"}},
|
||||
{:a => 2, :b => {:c => "a"}},
|
||||
{:a => 3},
|
||||
{:a => 3}])
|
||||
|
||||
assert_equal [0, 1, 2, 3], @@test.distinct(:a).sort
|
||||
assert_equal ["a", "b", "c"], @@test.distinct("b.c").sort
|
||||
end
|
||||
end
|
||||
|
||||
def test_safe_insert
|
||||
a = {"hello" => "world"}
|
||||
@@test.insert(a)
|
||||
|
|
Loading…
Reference in New Issue