What are the sizes returned by `show collections`?

13,076

Solution 1

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

Solution 2

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"] + ": " + stats[c]["size"] + " (" + (stats[c]["storageSize"] / 1073741824).toFixed(3) + "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)

Solution 3

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)`);
}
Share:
13,076
Jérôme
Author by

Jérôme

Breaking second law of thermodynamics? I don't mind. But PEP8... Wow, that's serious stuff.

Updated on June 17, 2022

Comments

  • Jérôme
    Jérôme almost 2 years

    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
    Jérôme over 7 years
    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
    Jérôme over 7 years
    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.
  • kevinadi
    kevinadi over 7 years
    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.
  • Jérôme
    Jérôme over 7 years
    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.
  • kevinadi
    kevinadi over 7 years
    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: 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.
  • Jérôme
    Jérôme over 7 years
    Right. I'll do that, it's much better. Thanks again.
  • thapakazi
    thapakazi over 7 years
    I use a tiny function on my servers, can't use vanilla console any more... github.com/thapakazi/kutto_kodalo/blob/gumantae/shellrc.d/…
  • Mugen
    Mugen almost 4 years
    for sizes in GB use print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize']/1073741824.0 + "GB)")