1.导出Mongodb 某个DB下的索引

1.1 导出索引的js脚本

说明:需要修改脚本  export_indexes_01.js  参数:

dbName 
[root@dbtbj mongodb]# cat export_indexes_01.js 
var dbName = "test";                              // 替换为你的目标数据库名

var db = db.getSiblingDB(dbName);                 // 连接到目标数据库
var collections = db.getCollectionNames();        // 获取数据库下所有集合

collections.forEach(function(colName) {           // 遍历每个集合,生成带 background: true 的索引创建语句
    var collection = db.getCollection(colName);
    var indexes = collection.getIndexes();        // 获取当前集合的所有索引
    
    indexes.forEach(function(index) {
        if (index.name === "_id_") return;        // 跳过 MongoDB 自动创建的 _id 索引(无需导出)
        
        var keyStr = JSON.stringify(index.key).replace(/"/g, "'");         // 1. 处理索引键(如 {x:1, name:1}),将双引号替换为单引号(可选,增强可读性)
        

        var options = {                           // 2. 处理索引选项:强制添加 background: true,保留原有的其他选项(如 unique、expireAfterSeconds 等)
            background: true                      // 核心:强制所有索引后台创建
        };

        if (index.unique) options.unique = index.unique;          // 保留原索引的其他配置(若存在)
        if (index.sparse) options.sparse = index.sparse;
        if (index.partialFilterExpression) options.partialFilterExpression = index.partialFilterExpression;
        if (index.expireAfterSeconds) options.expireAfterSeconds = index.expireAfterSeconds;
        if (index.collation) options.collation = index.collation;
        
        var optionsStr = ", " + JSON.stringify(options).replace(/"/g, "'");         // 3. 格式化选项字符串(若有多个选项,自动拼接)
        
        var createIndexCmd = "db." + colName + ".createIndex(" + keyStr + optionsStr + ");";         // 4. 生成最终的 createIndex 语句(格式:db.集合.createIndex(键, {background:true, ...}))
        print(createIndexCmd); // 输出到控制台,后续通过重定向保存
    });
});

print("\n==== 索引创建语句已生成,所有索引均包含 background: true ====");

1.2 导出示例

查看DB下面表的索引

cmgo-mt4ex5gp_0:PRIMARY> db.testcoll.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "x" : 1,
                        "name" : 1
                },
                "name" : "x_1_name_1"
        },
        {
                "v" : 2,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "background" : true
        },
        {
                "v" : 2,
                "key" : {
                        "name" : 1,
                        "_id_" : 1
                },
                "name" : "name_1__id__1",
                "background" : true
        }
]
cmgo-mt4ex5gp_0:PRIMARY> exit

导出实例

[root@dbtbj mongodb]# mongo --host=10.250.147.90   export_indexes_01.js > test.js
[root@dbtbj mongodb]# cat test.js 
MongoDB shell version v5.0.13
connecting to: mongodb://10.250.147.90:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f3455aa4-2c6c-43a8-b3f0-93476ee0f2c3") }
MongoDB server version: 4.4.13
WARNING: shell and server versions do not match
db.testcoll.createIndex({'x':1,'name':1}, {'background':true});
db.testcoll.createIndex({'name':1}, {'background':true});
db.testcoll.createIndex({'name':1,'_id_':1}, {'background':true});

==== 索引创建语句已生成,所有索引均包含 background: true ====
[root@dbtbj mongodb]# 

 

 posted on 2025-08-30 16:27  xibuhaohao  阅读(5)  评论(0)    收藏  举报