diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 71d1c50..e3a31ed 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -367,7 +367,14 @@ module Mongo if spec.is_a?(String) || spec.is_a?(Symbol) field_spec[spec.to_s] = 1 elsif spec.is_a?(Array) && spec.all? {|field| field.is_a?(Array) } - spec.each { |f| field_spec[f[0].to_s] = f[1] } + spec.each do |f| + if [Mongo::ASCENDING, Mongo::DESCENDING, Mongo::GEO2D].include?(f[1]) + field_spec[f[0].to_s] = f[1] + else + raise MongoArgumentError, "Invalid index field #{f[1].inspect}; " + + "should be one of Mongo::ASCENDING (1), Mongo::DESCENDING (-1) or Mongo::GEO2D ('2d')." + end + end else raise MongoArgumentError, "Invalid index specification #{spec.inspect}; " + "should be either a string, symbol, or an array of arrays." diff --git a/test/collection_test.rb b/test/collection_test.rb index f3dbfee..b18e0b8 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -577,6 +577,12 @@ class TestCollection < Test::Unit::TestCase end end + should "enforce proper index types" do + assert_raise MongoArgumentError do + @collection.create_index([['c', 'blah']]) + end + end + should "generate indexes in the proper order" do @collection.expects(:insert_documents) do |sel, coll, safe| assert_equal 'b_1_a_1', sel[:name]