Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB : Unable to drop a Compound Index on a collection

Tags:

mongodb

I have a compound index on my collection called people as shown below

 db.people.getIndexes()
[
        {
                "name" : "_id_",
                "ns" : "at.people",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "_id" : ObjectId("521dd652a185d3defe301983"),
                "ns" : "at.people",
                "key" : {
                        "personname" : 1,
                        "email" : 1,
                        "sex" : 1,
                        "course" : 1
                },
                "name" : "personname_1_email_1_sex_1_course_1",
                "unique" : false
        }
]

I am trying to drop this index this way

 db.people.dropIndex({"personname_1_email_1_sex_1_course_1": 1})

But i am getting error message as

{ "errmsg" : "index not found", "ok" : 0 }

I also tried to drop the index by name

db.people.dropIndex( { "name" : "personname_1_email_1_sex_1_course_1" } )

I know i can drop the indexes on a collection in one shot using the below command

db.people.dropIndexes()

Please let me know how to resolve this ?

like image 464
Pawan Avatar asked Sep 06 '13 12:09

Pawan


People also ask

Which index Cannot be deleted in MongoDB?

Default _id Index MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.

How do I drop a specific index in MongoDB?

Command Fields To drop a single index, specify either the index name, the index specification document (unless the index is a text index), or an array of the index name. To drop a text index, specify the index names instead of the index specification document.

Does MongoDB support compound indexes?

MongoDB supports compound indexes, where a single index structure holds references to multiple fields [1] within a collection's documents. The following diagram illustrates an example of a compound index on two fields: MongoDB imposes a limit of 32 fields for any compound index.

What is MongoDB compound index?

Or in other words, compound indexes are those indexes where a single index field contains references to multiple fields. In MongoDB, the compound index can contain a single hashed index field, if a field contains more than one hashed index field then MongoDB will give an error.


2 Answers

Pass the index name into dropIndex without putting it into an object:

db.people.dropIndex("personname_1_email_1_sex_1_course_1")
like image 112
JohnnyHK Avatar answered Nov 15 '22 04:11

JohnnyHK


Just to make full answer.

From documentation: http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/ you should use db.collection.dropIndex() with string or document that:

Specifies the index to drop. You can specify the index either by the index name or by the index specification document.

So both:

db.people.dropIndex("personname_1_email_1_sex_1_course_1")
db.people.dropIndex({personname:1,email:1,sex:1,course:1})

works fine.

like image 35
Yevgeniy Anfilofyev Avatar answered Nov 15 '22 04:11

Yevgeniy Anfilofyev