Skip to content

Instantly share code, notes, and snippets.

@ephrin
Created October 28, 2013 18:35
Show Gist options
  • Save ephrin/7202154 to your computer and use it in GitHub Desktop.
Save ephrin/7202154 to your computer and use it in GitHub Desktop.
var current = db.getName();
print('logged in from owner. Making auth.');
var adm = db.getSiblingDB('admin');
if(adm.auth('admin','admin')){
print('ok');
}else{
if(!adm.auth()){
}
}
var _prompt = function(){
var s = db.stats();
return db.getName() + ' [c:'+ s.collections + ', s:'+Number((s.dataSize/1024).toFixed(2))+'KB]'+' >'
};
function sizeStr(sizeBytes){
}
function cols(){
var colsStrings = [];
var colNames = new String(db.getCollectionNames()).split(',');
var longest = 0;
colNames.forEach(
function(colname){
if(colname.length > longest){
longest = colname.length;
}
}
);
var spaces = function(n){
return (new Array(n)).join(' ');
};
var human = function(size){
var sizeStr = '';
var sz = [['K',1024], ['M',Math.pow(1024,2)], ['G',Math.pow(1024,3)]];
for(var i = sz.length-1; i>-1; i--){
if(size/sz[i][1] >= 1){
sizeStr += Number(size/sz[i][1]).toFixed(0)+sz[i][0]+' ';
size = size%sz[i][1];
}
}
return sizeStr;
}
var prints = [];
var lns = [
0,0,0
];
colNames.forEach(function(colname){
var stats = db[colname].stats();
var pad = longest - colname.length + 2;
var s = spaces(pad);
var r = [
colname + s + '[size: ' + human(stats.storageSize),
'docs: ' + stats.count,
'indSize: '+ human(stats.totalIndexSize),
']'];
//td
prints.push(r);
});
prints.forEach(function(r){
for(var i=0; i<3; i++){
if(lns[i]<r[i].length){
lns[i] = r[i].length
}
}
});
print(lns);
prints.forEach(function(row){
var line = '';
var i = 0;
for(;i<3; i++){
line +=row[i]+spaces((lns[i]-row[i].length)+2);
}
line+=row[i];
print(line);
})
}
var $c = cols;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment