Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the sizes returned by `show collections`?

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.

like image 378
Jérôme Avatar asked Nov 28 '22 00:11

Jérôme


2 Answers

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)
like image 127
kalaivani Avatar answered Feb 17 '23 12:02

kalaivani


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

like image 26
kevinadi Avatar answered Feb 17 '23 12:02

kevinadi