目录
以下操作都是在admin数据库下执行
查询自定义索引
db.runCommand({listDatabases: 1})["databases"].forEach(
function(databaseinfo) {
if (databaseinfo['name'] == 'config' || databaseinfo['name'] == 'admin'){
return
}
db.getSiblingDB(databaseinfo['name']).getCollectionNames().forEach(
function(collname) {
db.getSiblingDB(databaseinfo['name'])[collname].getIndexes().forEach(
function(indexinfo) {
if(indexinfo['name'] == '_id_'){
return
}else{
printjson({"dbname": databaseinfo['name'], "collname": collname, "index": indexinfo['key']})
}
}
)
}
)
}
)
返回样例
{
"dbname" : "sa_show_account",
"collname" : "OperationLog",
"index" : {
"account" : 1,
"time" : 1
}
}
删除所有自定义索引(慎用):
db.runCommand({listDatabases: 1})["databases"].forEach(
function(databaseinfo) {
if (databaseinfo['name'] == 'config' || databaseinfo['name'] == 'admin'){
return
}
db.getSiblingDB(databaseinfo['name']).getCollectionNames().forEach(
function(collname) {
db.getSiblingDB(databaseinfo['name'])[collname].getIndexes().forEach(
function(indexinfo) {
if(indexinfo['name'] == '_id_'){
return
}else{
printjson({"dbname": databaseinfo['name'], "collname": collname, "index": indexinfo['key']})
db.getSiblingDB(databaseinfo['name']).getCollection(collname).dropIndex(indexinfo['name'])
}
}
)
}
)
}
)
根据第一步查询插入索引:
[{
"dbname" : "dc_config_server",
"collname" : "mongo_hit_sensitive_words",
"index" : {
"account_id" : 1
}
}
{
"dbname" : "popaccessserver",
"collname" : "TenantConfigRecord",
"index" : {
"tenant_id" : 1
}
}
{
"dbname" : "sa_show_account",
"collname" : "OperationLog",
"index" : {
"account" : 1,
"time" : 1
}
}
].forEach(
function(indexinfo) {
db.getSiblingDB(indexinfo['dbname']).getCollection(indexinfo['collname']).createIndex(indexinfo['index'])
}
)
根据步骤一的索引,删除索引
[
{
"dbname" : "dc_config_server",
"collname" : "mongo_hit_sensitive_words",
"index" : {
"account_id" : 1
}
},
{
"dbname" : "popaccessserver",
"collname" : "TenantConfigRecord",
"index" : {
"tenant_id" : 1
}
},
{
"dbname" : "sa_show_account",
"collname" : "OperationLog",
"index" : {
"account" : 1,
"time" : 1
}
}
].forEach(
function(indexinfo) {
// Convert the index object to a string representation of the index name
var indexName = Object.keys(indexinfo['index']).map(function(key) {
return key + "_" + indexinfo['index'][key];
}).join("_");
// Drop the index using the constructed index name
db.getSiblingDB(indexinfo['dbname']).getCollection(indexinfo['collname']).dropIndex(indexName);
}
);
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » mongodb 导入导出索引--查询慢问题
发表评论 取消回复