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 ?
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.
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.
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.
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.
Pass the index name into dropIndex
without putting it into an object:
db.people.dropIndex("personname_1_email_1_sex_1_course_1")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With