Last active
March 3, 2017 06:57
-
-
Save RavenZZ/fa2a8366940bafe633ed9af635d1e5f8 to your computer and use it in GitHub Desktop.
根据Mongodb已有索引生成 建立索引脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var db = connect(url); | |
var collections = db.getCollectionNames(); | |
collections.forEach((table)=>{ | |
var commands = []; | |
var indexes = db[table].getIndexes(); | |
if(indexes.length>0){ | |
print(`//collection: ${table}`) | |
} | |
indexes.forEach((index)=>{ | |
if(index.name!="_id_"){ | |
var keys = JSON.stringify(index.key); | |
var options = {}; | |
Object.keys(index).forEach((k)=>{ | |
if(["v","key","ns"].indexOf(k)==-1){ | |
options[k] = index[k]; | |
} | |
}) | |
var paramsString = JSON.stringify(options) | |
var cmd = `db.${table}.ensureIndex(${keys},${paramsString});` | |
print(cmd); | |
} | |
}); | |
print("\n"); | |
}) | |
print("\n"); | |
print("end"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nohup mongo --eval "var url='127.0.0.1:27017/taskcenter';" /usr/local/mongoshell/gen.js > allindex.js & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
执行结果如图