top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

MongoDB: What is the command to remove indexes from a collection in mongoDB ?

+3 votes
436 views
MongoDB: What is the command to remove indexes from a collection in mongoDB ?
posted Jul 4, 2015 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

dropIndex( ) , is used to remove the index from a collection.
By default, mongoDB system creates an index on _id when first document is inserted into collection.
As per my understanding of mongoDB, a user can remove indexes which are created by user. I meant to say, index on _id can't be removed.

When I tried to remove index on _id, system throws following error.

"nIndexesWas" : 1, "ok" : 0, "errmsg" : "cannot drop _id index" }

answer Jul 4, 2015 by Alok
Similar Questions
+3 votes

Here I would like to explain my query a bit more.

I inserted multiple documents within a collection. Few documents have same set of keys and few of them have some extra key:value pairs . For example:
document 1 : key1 : "key1"
document 2: key1 : "key1", key2: "key2"
document 3: key1 : "key1", key2: "key2", key3 : "key3"
document 4: key1 : "key1", key3 : "key3"

Now I want to delete the documents which have key1 and key2.
What will be the command to do so ?

...