Skip to content

Instantly share code, notes, and snippets.

@ephrin
Last active December 26, 2015 19:39
Show Gist options
  • Save ephrin/7202800 to your computer and use it in GitHub Desktop.
Save ephrin/7202800 to your computer and use it in GitHub Desktop.
$c([sort]) Useful for database collections quick information overview. Examples: $c({size:1}) //ascendance sorting by size $c({indSize:-1}) //desc sorting by index size $c({docs:1}) // sorts by documents count output
function cols(sort){
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 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),
']',
{
'size':stats.storageSize,
'docs':stats.count,
'indSize':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
}
}
});
if(sort){
for(var by in sort){
if(sort.hasOwnProperty(by)){
prints.sort(function(arr1, arr2){
if(arr1[4].hasOwnProperty(by) && arr2[4].hasOwnProperty(by)){
if(sort[by]>0){
return arr1[4][by]-arr2[4][by];
}
if(sort[by]<0){
return arr2[4][by]-arr1[4][by];
}
return 0;
}else{
return 0;
}
});
}
}
}
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;
h24rs0:PRIMARY> $c({indSize:-1})
calendar [size: 1G 189M 620K docs: 377566 indSize: 20M 1000K ]
rooms [size: 63M 80K docs: 14758 indSize: 375K ]
seo [size: 11M 680K docs: 4394 indSize: 287K ]
reviews [size: 11M 680K docs: 6089 indSize: 160K ]
hotels [size: 63M 760K docs: 3749 indSize: 104K ]
user.visits [size: 40K docs: 192 indSize: 48K ]
cities [size: 5M 944K docs: 609 indSize: 24K ]
system.users [size: 8K docs: 3 indSize: 16K ]
hotel.types [size: 40K docs: 30 indSize: 8K ]
user.visits.sys [size: 8K docs: 0 indSize: 8K ]
countries [size: 168K docs: 120 indSize: 8K ]
regions [size: 168K docs: 36 indSize: 8K ]
hotel.agreements [size: 40K docs: 8 indSize: 8K ]
room.facilities [size: 40K docs: 54 indSize: 8K ]
hotel.descriptionTypes [size: 8K docs: 11 indSize: 8K ]
hotel.descriptions [size: 8K docs: 12 indSize: 8K ]
hotel.facilities [size: 8K docs: 19 indSize: 8K ]
hotel.formsOfActivity [size: 8K docs: 5 indSize: 8K ]
test_custom [size: 8K docs: 9 indSize: 8K ]
id [size: 8K docs: 12 indSize: 8K ]
system.indexes [size: 8K docs: 26 indSize: ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment