Skip to content

Commit 408fbc1

Browse files
committed
Merge pull request mongodb#44 from pto/master
Allow drop_dups on ensure_index as well as on create_index
2 parents 029e264 + 809dcf7 commit 408fbc1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/mongo/collection.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ def hint=(hint=nil)
163163
# This is to prevent any inadvertant failure to close the cursor, as the cursor is explicitly
164164
# closed when block code finishes.
165165
# @option opts [Integer] :max_scan (nil) Limit the number of items to scan on both collection scans and indexed queries..
166-
# @option opts [Boolean] :show_disk_loc (false) Return the disk location of each query result (for debugging).
167-
# @option opts [Boolean] :return_key (false) Return the index key used to obtain the result (for debugging).
168166
# @option opts [Block] :transformer (nil) a block for tranforming returned documents.
169167
# This is normally used by object mappers to convert each returned document to an instance of a class.
170168
#
@@ -470,6 +468,7 @@ def create_index(spec, opts={})
470468
# @return [String] the name of the index.
471469
def ensure_index(spec, opts={})
472470
now = Time.now.utc.to_i
471+
opts[:dropDups] = opts[:drop_dups] if opts[:drop_dups]
473472
field_spec = parse_index_spec(spec)
474473

475474
name = opts[:name] || generate_index_name(field_spec)

test/collection_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,14 @@ def test_ensure_index_timeout
809809
assert_equal 1, @collection.find({:a => 1}).count
810810
end
811811

812+
should "drop duplicates with ensure_index and drop_dups key" do
813+
@collection.insert({:a => 1})
814+
@collection.insert({:a => 1})
815+
assert_equal 2, @collection.find({:a => 1}).count
816+
@collection.ensure_index([['a', Mongo::ASCENDING]], :unique => true, :drop_dups => true)
817+
assert_equal 1, @collection.find({:a => 1}).count
818+
end
819+
812820
should "create an index in the background" do
813821
if @@version > '1.3.1'
814822
@collection.create_index([['b', Mongo::ASCENDING]], :background => true)

0 commit comments

Comments
 (0)