Last active
July 17, 2017 04:44
-
-
Save RavenZZ/fc830cdb3a0f8a2e52eb17404419ede4 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 dbName = db.getName(); | |
print(`Database: ${dbName}`); | |
var collections = db.getCollectionNames() | |
collections.forEach((table)=>{ | |
var commands = []; | |
var indexes = db[table].getIndexes(); | |
if(indexes.length>0){ | |
print(`//collection: ${table}`) | |
var indexList = []; | |
indexes.forEach((index)=>{ | |
if(index.name!="_id_"){ | |
var keys = index.key; | |
var options = {}; | |
Object.keys(index).forEach((k)=>{ | |
if(["v","key","ns"].indexOf(k)==-1){ | |
options[k] = index[k]; | |
} | |
}) | |
var indexItem = options; | |
indexItem["key"] = keys; | |
indexList.push(indexItem) | |
} | |
}); | |
var cmd = `db.runCommand({createIndexes:"${table}",indexes:${JSON.stringify(indexList)}});` | |
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 & |
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 conn = new Mongo(url); | |
var databases = conn.getDBNames(); | |
databases.forEach((dbName)=>{ | |
print("\n"); | |
print(`# Database: ${dbName}`); | |
print("\n"); | |
db = conn.getDB(dbName); | |
var collections = db.getCollectionNames() | |
collections.forEach((table)=>{ | |
var commands = []; | |
var indexes = db[table].getIndexes(); | |
if(indexes.length>0){ | |
print(`## collection: ${table}`) | |
print("\n"); | |
print(`### Entity`); | |
print("\n"); | |
print("```javascript\n"); | |
print("```"); | |
var indexList = []; | |
indexes.forEach((index)=>{ | |
if(index.name!="_id_"){ | |
var keys = index.key; | |
var options = {}; | |
Object.keys(index).forEach((k)=>{ | |
if(["v","key","ns"].indexOf(k)==-1){ | |
options[k] = index[k]; | |
} | |
}) | |
var indexItem = options; | |
indexItem["key"] = keys; | |
indexList.push(indexItem) | |
} | |
}); | |
print("\n"); | |
print(`### index`); | |
print("```javascript\n"); | |
var cmd = `db.runCommand({createIndexes:"${table}",indexes:${JSON.stringify(indexList)}});` | |
print(cmd); | |
print("```"); | |
print("\n"); | |
} | |
}) | |
print("\n"); | |
print("========================="); | |
}); | |
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 conn = new Mongo(url); | |
var databases = conn.getDBNames(); | |
databases.forEach((dbName)=>{ | |
print("\n"); | |
print(`Database: ${dbName}`); | |
db = conn.getDB(dbName); | |
var collections = db.getCollectionNames() | |
collections.forEach((table)=>{ | |
var commands = []; | |
var indexes = db[table].getIndexes(); | |
if(indexes.length>0){ | |
print(`//collection: ${table}`) | |
var indexList = []; | |
indexes.forEach((index)=>{ | |
if(index.name!="_id_"){ | |
var keys = index.key; | |
var options = {}; | |
Object.keys(index).forEach((k)=>{ | |
if(["v","key","ns"].indexOf(k)==-1){ | |
options[k] = index[k]; | |
} | |
}) | |
var indexItem = options; | |
indexItem["key"] = keys; | |
indexList.push(indexItem) | |
} | |
}); | |
var cmd = `db.runCommand({createIndexes:"${table}",indexes:${JSON.stringify(indexList)}});`; | |
print(cmd); | |
print("\n"); | |
} | |
}) | |
print("\n"); | |
print("========================="); | |
}); | |
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/';" /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