to be
a problem slover

mongoDB indexes migration

问题描述

如何把 mongoDB 中某个 collection 的 indexes 应用到另一个相似的 collection 上面?

首先可以通过这个原生的方法查看 index 列表

db.your_collection.getIndexes();

结果

但问题是这并不是可执行的语句,如何能像 mysql 一样导出创建 index 的语句,以便可以应用在新表上?

解决办法

根据我简单的 research 发现没有原生的方法可以实现,但是mongosh可以通过执行js script 来实现。

Step0: 新建/tmp/script.js 文件

var collectionList = db.getCollectionNames();
// var collectionList = ["foo","bar"]
for (var index in collectionList) {
  var collection = collectionList[index];
  var cur = db.getCollection(collection).getIndexes();
  if (cur.length == 1) {
    continue;
  }
  for (var index1 in cur) {
    var next = cur[index1];
    if (next["name"] == "_id_") {
      continue;
    }
    var unique = next["unique"] ? true : false;
    print(
      'db.getCollection("' +
        collection +
        '").createIndex(' +
        JSON.stringify(next["key"]) +
        ",{unique:" +
        unique +
        "},{background:1});"
    );
  }
}

Step1: 通过mongosh执行 script

mongosh --quiet 'mongodb://$user:$passwd@$host:$port/$db?ssl=false&authSource=admin' /tmp/script.js > /tmp/result.txt

结果直接是可执行的 mongo 语句

db.getCollection("foo").createIndex({"created_time":1,"contract":1,"symbol":1},{unique:false},{background:1});
db.getCollection("foo").createIndex({"contract":1,"symbol":1},{unique:false},{background:1});
db.getCollection("foo").createIndex({"created_time":1},{unique:false},{background:1});

Step2: 根据自己的需求更新语句,然后应用在新 collection 上面

赞(6) 打赏
欢迎转载,注明出处:刘世明的博客 » mongoDB indexes migration

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏