Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Last active March 3, 2017 06:57
Show Gist options
  • Save RavenZZ/fa2a8366940bafe633ed9af635d1e5f8 to your computer and use it in GitHub Desktop.
Save RavenZZ/fa2a8366940bafe633ed9af635d1e5f8 to your computer and use it in GitHub Desktop.
根据Mongodb已有索引生成 建立索引脚本
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");
nohup mongo --eval "var url='127.0.0.1:27017/taskcenter';" /usr/local/mongoshell/gen.js > allindex.js &
@RavenZZ
Copy link
Author

RavenZZ commented Jul 5, 2016

执行结果如图
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment