Merge pull request #46 from karlseguin/master
collection.drop_index([[SPEC]]) support
This commit is contained in:
commit
b496f23b75
|
@ -497,6 +497,9 @@ module Mongo
|
|||
#
|
||||
# @core indexes
|
||||
def drop_index(name)
|
||||
if name.is_a?(Array)
|
||||
return drop_index(index_name(name))
|
||||
end
|
||||
@cache[name.to_s] = nil
|
||||
@db.drop_index(@name, name)
|
||||
end
|
||||
|
@ -825,6 +828,14 @@ module Mongo
|
|||
|
||||
private
|
||||
|
||||
def index_name(spec)
|
||||
field_spec = parse_index_spec(spec)
|
||||
index_information.each do |index|
|
||||
return index[0] if index[1]['key'] == field_spec
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def parse_index_spec(spec)
|
||||
field_spec = BSON::OrderedHash.new
|
||||
if spec.is_a?(String) || spec.is_a?(Symbol)
|
||||
|
|
|
@ -763,6 +763,41 @@ class TestCollection < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "Drop index " do
|
||||
setup do
|
||||
@@db.drop_collection('test-collection')
|
||||
@collection = @@db.collection('test-collection')
|
||||
end
|
||||
|
||||
should "drop an index" do
|
||||
@collection.create_index([['a', Mongo::ASCENDING]])
|
||||
assert @collection.index_information['a_1']
|
||||
@collection.drop_index([['a', Mongo::ASCENDING]])
|
||||
assert_nil @collection.index_information['a_1']
|
||||
end
|
||||
|
||||
should "drop an index which was given a specific name" do
|
||||
@collection.create_index([['a', Mongo::DESCENDING]], {:name => 'i_will_not_fear'})
|
||||
assert @collection.index_information['i_will_not_fear']
|
||||
@collection.drop_index([['a', Mongo::DESCENDING]])
|
||||
assert_nil @collection.index_information['i_will_not_fear']
|
||||
end
|
||||
|
||||
should "drops an composite index" do
|
||||
@collection.create_index([['a', Mongo::DESCENDING], ['b', Mongo::ASCENDING]])
|
||||
assert @collection.index_information['a_-1_b_1']
|
||||
@collection.drop_index([['a', Mongo::DESCENDING], ['b', Mongo::ASCENDING]])
|
||||
assert_nil @collection.index_information['a_-1_b_1']
|
||||
end
|
||||
|
||||
should "drops an index with symbols" do
|
||||
@collection.create_index([['a', Mongo::DESCENDING], [:b, Mongo::ASCENDING]])
|
||||
assert @collection.index_information['a_-1_b_1']
|
||||
@collection.drop_index([['a', Mongo::DESCENDING], [:b, Mongo::ASCENDING]])
|
||||
assert_nil @collection.index_information['a_-1_b_1']
|
||||
end
|
||||
end
|
||||
|
||||
context "Creating indexes " do
|
||||
setup do
|
||||
@@db.drop_collection('geo')
|
||||
|
|
Loading…
Reference in New Issue