Merge pull request #46 from karlseguin/master

collection.drop_index([[SPEC]]) support
This commit is contained in:
Kyle Banker 2011-06-03 11:41:04 -07:00
commit b496f23b75
2 changed files with 46 additions and 0 deletions

View File

@ -497,6 +497,9 @@ module Mongo
# #
# @core indexes # @core indexes
def drop_index(name) def drop_index(name)
if name.is_a?(Array)
return drop_index(index_name(name))
end
@cache[name.to_s] = nil @cache[name.to_s] = nil
@db.drop_index(@name, name) @db.drop_index(@name, name)
end end
@ -824,6 +827,14 @@ module Mongo
end end
private 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) def parse_index_spec(spec)
field_spec = BSON::OrderedHash.new field_spec = BSON::OrderedHash.new

View File

@ -762,6 +762,41 @@ class TestCollection < Test::Unit::TestCase
assert_equal 1, @collection.size assert_equal 1, @collection.size
end end
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 context "Creating indexes " do
setup do setup do