[SERVER-1887] dropIndex not working if a name was specified during the ensureIndex Created: 05/Oct/10  Updated: 04/Feb/15  Resolved: 09/Oct/12

Status: Closed
Project: Core Server
Component/s: Admin, Usability
Affects Version/s: 1.6.0
Fix Version/s: 2.2.2, 2.3.0

Type: Bug Priority: Minor - P4
Reporter: Alvin Richards (Inactive) Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Issue Links:
Depends
Related
related to DOCS-596 dropIndex() by index name is not ment... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

Problem:
If an index is created with a name, then dropIndex will fail to drop the index. However, runCommand(

{dropIndexes...}) succeeds.

Reproduce:
Do the following

> db.journeys.save( {type :1, tm_uuid:2} )                            
> db.journeys.ensureIndex({type:1, tm_uuid:1}, {name:"Type TM-UUID"}); 
 
> db.journeys.getIndexes() 
[
	{
		"name" : "_id_",
		"ns" : "test.journeys",
		"key" : {
			"_id" : 1
		}
	},
	{
		"_id" : ObjectId("4cab0f09f29d24000f38d92f"),
		"ns" : "test.journeys",
		"key" : {
			"type" : 1,
			"tm_uuid" : 1
		},
		"name" : "Type TM-UUID"
	}
]
 
 
> db.journeys.dropIndex({name:"Type TM-UUID"});                      
{ "errmsg" : "index not found", "ok" : 0 }
 
 
> db.runCommand({dropIndexes:'journeys', index : {type:1,tm_uuid:1}})
{ "nIndexesWas" : 2, "ok" : 1 }
 
 
> db.journeys.getIndexes()                                           
[
	{
		"name" : "_id_",
		"ns" : "test.journeys",
		"key" : {
			"_id" : 1
		}
	}
]


Workaround:
Use db.runCommand({dropIndexes...}

) to drop a index with a name.

Business Case:
User Expereince



 Comments   
Comment by auto [ 06/Nov/12 ]

Author:

{u'date': u'2012-10-09T09:41:27Z', u'email': u'tad@10gen.com', u'name': u'Tad Marshall'}

Message: SERVER-1887 make db.coll.dropIndex() work with name or key object

Remove the fixup of the user's index specification that was converting
it into a default-style index name. Adjust comments in code and help text
to describe how to use db.collectionName.dropIndex().
Branch: v2.2
https://github.com/mongodb/mongo/commit/1ce89e9ff2dad6b0dade79bd378d5ad4b765c0e3

Comment by Tad Marshall [ 09/Oct/12 ]

Docs should mention that the shell's dropIndex() method on collections accepts two syntaxes:

db.collectionName.dropIndex( "indexName" )
db.collectionName.dropIndex(

{ "indexKey" : 1 }

)

Both of these should work in all versions if no explicit name was specified when the index was created. If a name was specified, only the first syntax will work until this fix is in the code.

Comment by auto [ 09/Oct/12 ]

Author:

{u'date': u'2012-10-09T02:41:27-07:00', u'email': u'tad@10gen.com', u'name': u'Tad Marshall'}

Message: SERVER-1887 make db.coll.dropIndex() work with name or key object

Remove the fixup of the user's index specification that was converting
it into a default-style index name. Adjust comments in code and help text
to describe how to use db.collectionName.dropIndex().
Branch: master
https://github.com/mongodb/mongo/commit/ed821b6060eacc995740c233ec30899b80a717b0

Comment by Tad Marshall [ 09/Oct/12 ]

The example given of using dropIndex isn't the correct syntax for this helper function. To drop an index by name, you should use:

db.journeys.dropIndex("Type TM-UUID")

The way that you could do this for an index without a specified name,

db.journeys.dropIndex({type:1, tm_uuid:1})

fails if the index has a specified name that doesn't match the autogenerated name, but this can be fixed in the helper function.

Generated at Thu Feb 08 02:58:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.