mongoshell启动配置文件.mongorc.js(三)-创新互联

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

目前创新互联公司已为近千家的企业提供了网站建设、域名、网站空间、网站托管、服务器托管、企业网站设计、武昌网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

自定义MongoDB操作函数

可以把自己写的js代码保存在某个地方,让MongoDB加载它,然后就可以在MongoDB的命令行里操作它们。

mongodb shell默认会加载~/.mongorc.js文件

例如以下修改了启动提示文字、左侧提示文字,增加了my_show_shards shell函数用于显示当前sharded collection的chunks在各分片的负载情况:

//~/.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[index] + " 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[JSON.stringify(next_item["ns"]).replace(/\"/g, "")] = 1;             shards[JSON.stringify(next_item["shard"]).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());                     })                 })     }

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:mongoshell启动配置文件.mongorc.js(三)-创新互联
网站链接:http://scyanting.com/article/csdgpj.html