Drop all indexes from all collections in a MongoDB database using the command line
Your command works for me (it drops all indexes on the currently selected DB). But you can also use this alternative.
db.getCollectionNames().forEach(function(collName) { db.runCommand({dropIndexes: collName, index: "*"});});
When dropping indexes only non _id
indexes will be dropped.
Workaround solution is to drop the database and set --noIndexRestore
flag when restoring with mongorestore
so that the indexes are not restored.
From man mongorestore
:
--noIndexRestore
New in version 2.2.
Prevents mongorestore from restoring and building indexes as specified in the corresponding mongodump output.
You can use this command to drop all the indexes from all the collections:
db.getCollectionNames().forEach(function (d) { db[d].dropIndexes();});
Try this!