0
点赞
收藏
分享

微信扫一扫

mongodb 批量添加索引, 已test开头的集合

GG_lyf 2023-04-16 阅读 64


 

// To add an index to all collections in the database that start with "test" and have a field named "ts", and to run the operation in the background, use the following command:

db.getCollectionNames().forEach(function(collname) {
  if (collname.startsWith("test")) {
    db.getCollection(collname).createIndex({ts: 1}, {background: true});
  }
});

 

 

 

db.getCollectionNames().forEach(function(collname) {
  if (collname.startsWith("test")) {
    if (!db.getCollection(collname).getIndexes().some(function(index) { return index.name === "ts_1"; })) {
      db.getCollection(collname).createIndex({ts: 1}, {background: true});
    }
  }
});

 



举报

相关推荐

0 条评论