Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Last active July 17, 2017 04:44
Show Gist options
  • Save RavenZZ/fc830cdb3a0f8a2e52eb17404419ede4 to your computer and use it in GitHub Desktop.
Save RavenZZ/fc830cdb3a0f8a2e52eb17404419ede4 to your computer and use it in GitHub Desktop.
生成Mongodb索引建立脚本
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");
nohup mongo --eval "var url='127.0.0.1:27017/taskcenter';" /usr/local/mongoshell/gen.js > allindex.js &
//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("=========================");
});
//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("=========================");
});
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