您好,登錄后才能下訂單哦!
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()); }) }) }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。