fish3129 发表于 2018-8-25 07:03:11

mongo shell启动配置文件.mongorc.js(三)

//~/.mongorc.js  
//show at begin
  
var compliment = ["attractive", "intelligent", "like batman"];
  
var index = Math.floor(Math.random()*3);
  
print("Hello, you're looking particularly " + compliment + " today!");
  
//change the prompt
  
prompt = function(){
  
    if (typeof db == "undefined") {
  
      return "(nodb)> ";
  
    }
  
    // Check the last db operation
  
    try {
  
      db.runCommand({getLastError: 1});
  
    }
  
    catch (e) {
  
      print(e);
  
    }
  
    return db + "> ";
  
}
  
//show all shard's chunks
  
function my_show_shards() {
  
    var config_db = db.getSiblingDB("config");
  
    var collections = {};
  
    var shards = {};
  
    var shard_it = config_db.chunks.find().snapshot();
  
    while (shard_it.hasNext()) {
  
      next_item = shard_it.next();
  
      collections).replace(/\"/g, "")] = 1;
  
      shards).replace(/\"/g, "")] = 1;
  
    }
  
    var list_collections = [];
  
    var list_shards = [];
  
    for (item in collections) {
  
      list_collections.push(item);
  
    }
  
    for (item in shards) {
  
      list_shards.push(item);
  
    }
  
    list_collections.forEach(function(collec) {
  
            list_shards.forEach(function(item) {
  
                obj = {};
  
                obj["shard"] = item;
  
                obj["ns"] = collec;
  
                it = config_db.chunks.find(obj);
  
                print(collec, item, it.count());
  
                })
  
            })
  
}


页: [1]
查看完整版本: mongo shell启动配置文件.mongorc.js(三)