mongodb export index (table structure)

Export index script

Compatible with unique index, and timeout configuration

var collectionList = db.getCollectionNames();
for(var index in collectionList){
    var collection = collectionList[index];
    var cur = db.getCollection(collection).getIndexes();
    if(cur.length == 1){
        continue;
    }
    for(var index1 in cur){
    var next = cur[index1];
    if(next["key"]["_id"] == '1'){
        continue;
    }
    print(
    "try{ db.getCollection(\""+collection+"\").ensureIndex("+JSON.stringify(next.key)+",{background:1, unique:" + (next.unique || false) + "" + (next.expireAfterSeconds > 0 ? ", expireAfterSeconds :" + next.expireAfterSeconds  : "") + " })}catch(e){print(e)}")}}


Please give me a compliment

thank

Published 17 original articles · won 24 · views 280,000 +

Guess you like

Origin blog.csdn.net/qq_22956867/article/details/102917841