सभी संग्रहों पर पुनरावृत्त करके इंडेक्स ड्रॉप करना
कमांड लाइन से एक डेटाबेस के सभी संग्रहों से सभी इंडेक्स ( _id को छोड़कर) हटाने के लिए getCollectionNames() और runCommand() का उपयोग करें।
सिंटैक्स
db.getCollectionNames().forEach(function(yourVariableName) {
db.runCommand({dropIndexes: yourVariableName, index: "*"});
});
उदाहरण: इंडेक्स ड्रॉप करने से पहले जांचना
> db.indexingDemo.getIndexes();
आउटपुट: _id इंडेक्स और StudentFavouriteSubject पर एक कस्टम इंडेक्स दिखाता है।
सभी इंडेक्स ड्रॉप करना
> db.getCollectionNames().forEach(function(allCollectionName) {
db.runCommand({dropIndexes: allCollectionName, index: "*"});
});
सत्यापन
> db.indexingDemo.getIndexes();
आउटपुट: केवल _id इंडेक्स शेष रहता है (इसे ड्रॉप नहीं किया जा सकता)।
महत्वपूर्ण:
index: "*"सभी गैर-_id इंडेक्स ड्रॉप करता है।_idइंडेक्स अनिवार्य है और इसे ड्रॉप नहीं किया जा सकता।- यह ऑपरेशन वर्तमान डेटाबेस के सभी संग्रहों को प्रभावित करता है।
- उत्पादन में सावधानी से उपयोग करें; इंडेक्स पुनर्निर्माण महंगा हो सकता है।