7

Edit: This question is not about vanilla MongoDB's show collections but about mongo-hacker. See accepted answer and comments.


Using Mongo DB 3.2 + WiredTiger, show collections displays two sizes: s1 / s2.

show collections
coll_1               → 10.361MB / 1.289MB
coll_2               →  0.000MB / 0.004MB
coll_3               →  0.000MB / 0.016MB
coll_4               →  0.001MB / 0.031MB

My guess is these are:

  • s1: total size of the documents in the database
  • s2: size of the database on disk (documents + indexes) after compression

Is this correct? I couldn't find any reference in the docs.

Jérôme
  • 13,328
  • 7
  • 56
  • 106

3 Answers3

32

You can use the below query to get the size of each collections:

var collectionNames = db.getCollectionNames(),
  stats = [];
collectionNames.forEach(function (n) {
  stats.push(db[n].stats());
});
for (var c in stats) {
  // skip views
  if (!stats[c]["ns"]) continue;
  print(stats[c]["ns"].padEnd(40) + ": " + (''+stats[c]["size"]).padEnd(12) + " (" + (stats[c]["storageSize"] / 1073741824).toFixed(3).padStart(8) + "GB)");
}

Example putout:

cod-prod-db.orders: 35179407 (0.012GB)
cod-prod-db.system.profile: 4323 (0.000GB)
cod-prod-db.users: 21044037 (0.015GB)
Claudiu
  • 224,032
  • 165
  • 485
  • 680
kalaivani
  • 484
  • 1
  • 5
  • 8
11

Are you using mongo-hacker? By default in MongoDB 3.2.11, show collections doesn't show any size information at all.

The size information provided by mongo-hacker is obtained from the output of db.collection.stats().size which shows you the total uncompressed size of the collection (without indexes), and db.collection.stats().storageSize which shows you the physical storage size. If you enable compression in WiredTiger, the storageSize will typically be smaller than size.

You can find the relevant source code in here: https://github.com/TylerBrock/mongo-hacker/blob/0.0.13/hacks/show.js#L57-L72

kevinadi
  • 13,365
  • 3
  • 33
  • 49
  • So that would be what I defined as "total size of the documents" and "size on disk (documents + indexes)". Thanks for the source code. It looks pretty much like this. However, I never heard about mongo-hacker. I'm using MongoDB Debian packages from Mongo repository. Don't we have the same default Mongo? – Jérôme Dec 01 '16 at 09:04
  • OK, I get it. I copied `.mongorc.js` from my colleague to get colorized shell and it happens to include mongo-hacker stuff. I don't remember installing mongo-hacker specifically but apparently it works. Thanks. – Jérôme Dec 01 '16 at 09:10
  • Yes, mongo-hacker works by creating a .mongorc.js in your home directory. I guess you can "accidentally" use mongo-hacker if you copied someone's .mongorc.js file. Note that you can run mongo shell with `mongo --norc` to ignore the rc file. – kevinadi Dec 01 '16 at 22:38
  • I do mean to use this .rc file. I just copied it without checking what was inside (it comes from a "trusted zone"...) and I thought it was mostly about colours, I didn't think it would affect those outputs. But it's nice and I'm glad it does. BTW, the reason I don't remember installing mongo-hacker is that my colleague wanted to avoid using npm (we're from Python world, JS is hostile territory) so he basically concatenated mongo-hacker files into a single .rc file. Might not be best practice, but that's another story... Thanks anyway for sorting this out. – Jérôme Dec 01 '16 at 22:57
  • 1
    Glad to help. Actually you don't need npm to use mongo-hacker. It's basically a github repo, and you install it using the `make install` command, which essentially does what you described (concat assorted files into one rc file). Here's a link to the site: http://tylerbrock.github.io/mongo-hacker/, which also lists additional features that comes by default with it. I highly recommend it, since you can add/remove "hacks" as you see fit. – kevinadi Dec 01 '16 at 23:15
  • Right. I'll do that, it's much better. Thanks again. – Jérôme Dec 05 '16 at 13:40
  • I use a tiny function on my servers, can't use vanilla console any more... https://github.com/thapakazi/kutto_kodalo/blob/gumantae/shellrc.d/utils.sh#L96-L102 – thapakazi Jan 02 '17 at 08:24
6

Very similar to @kalaivani's answer, I just refactored it for easier (for me) understanding and also printing in GB

// Get collection names
var collectionNames = db.getCollectionNames()
var col_stats = [];

// Get stats for every collections
collectionNames.forEach(function (n) { 
    col_stats.push(db.getCollection(n).stats());
});

// Print
for (var item of col_stats) {
    print(`${item['ns']} | size: ${item['size']} 
(${(item['size']/1073741824).toFixed(2)} GB) | storageSize: ${item['storageSize']} 
(${(item['storageSize']/1073741824).toFixed(2)} GB)`);
}
Lelo
  • 854
  • 11
  • 25