Added query option for Collection#distinct
This commit is contained in:
parent
570305c46b
commit
f633e8ad69
@ -441,11 +441,17 @@ EOS
|
|||||||
# [10010, 94108, 99701]
|
# [10010, 94108, 99701]
|
||||||
# @collection.distinct("name.age")
|
# @collection.distinct("name.age")
|
||||||
# [27, 24]
|
# [27, 24]
|
||||||
def distinct(key)
|
#
|
||||||
|
# You may also pass a document selector as the second parameter
|
||||||
|
# to limit the documents over which distinct is run:
|
||||||
|
# @collection.distinct("name.age", {"name.age" => {"$gt" => 24}})
|
||||||
|
# [27]
|
||||||
|
def distinct(key, query=nil)
|
||||||
raise MongoArgumentError unless [String, Symbol].include?(key.class)
|
raise MongoArgumentError unless [String, Symbol].include?(key.class)
|
||||||
command = OrderedHash.new
|
command = OrderedHash.new
|
||||||
command[:distinct] = @name
|
command[:distinct] = @name
|
||||||
command[:key] = key.to_s
|
command[:key] = key.to_s
|
||||||
|
command[:query] = query
|
||||||
|
|
||||||
@db.command(command)["values"]
|
@db.command(command)["values"]
|
||||||
end
|
end
|
||||||
|
@ -75,17 +75,29 @@ class TestCollection < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
if @@version > "1.1"
|
if @@version > "1.1"
|
||||||
def test_distinct
|
context "distinct queries" do
|
||||||
@@test.remove
|
setup do
|
||||||
@@test.insert([{:a => 0, :b => {:c => "a"}},
|
@@test.remove
|
||||||
{:a => 1, :b => {:c => "b"}},
|
@@test.insert([{:a => 0, :b => {:c => "a"}},
|
||||||
{:a => 1, :b => {:c => "c"}},
|
{:a => 1, :b => {:c => "b"}},
|
||||||
{:a => 2, :b => {:c => "a"}},
|
{:a => 1, :b => {:c => "c"}},
|
||||||
{:a => 3},
|
{:a => 2, :b => {:c => "a"}},
|
||||||
{:a => 3}])
|
{:a => 3},
|
||||||
|
{:a => 3}])
|
||||||
|
end
|
||||||
|
|
||||||
assert_equal [0, 1, 2, 3], @@test.distinct(:a).sort
|
should "return distinct values" do
|
||||||
assert_equal ["a", "b", "c"], @@test.distinct("b.c").sort
|
assert_equal [0, 1, 2, 3], @@test.distinct(:a).sort
|
||||||
|
assert_equal ["a", "b", "c"], @@test.distinct("b.c").sort
|
||||||
|
end
|
||||||
|
|
||||||
|
should "filter collection with query" do
|
||||||
|
assert_equal [2, 3], @@test.distinct(:a, {:a => {"$gt" => 1}}).sort
|
||||||
|
end
|
||||||
|
|
||||||
|
should "filter nested objects" do
|
||||||
|
assert_equal ["a", "b"], @@test.distinct("b.c", {"b.c" => {"$ne" => "c"}}).sort
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user