This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Compute chunks size in mongodb

By: a guest on Sep 18th, 2012  |  syntax: JavaScript  |  size: 1.00 KB  |  views: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //works only for mongo 2.2
  2. //works better if you use mongo-hacker (https://github.com/TylerBrock/mongo-hacker)
  3. var ns ="products.products";
  4. var key = {partnerId : 1, id : 1}; //the shard key of the collection
  5. if (typeof colorize != "function") colorize = function(s) {return s};
  6. var zeroChunk =0
  7. var chunkSize = db.getSiblingDB("config").settings.findOne({_id : "chunksize"}).value; //target chunkSize in mb
  8. db.getSiblingDB("config").chunks.find({ns : ns}).limit(500).forEach(function(chunk) {
  9.         var ds = db.getSiblingDB(ns.split(".")[0]).runCommand({datasize:chunk.ns,keyPattern:key,min:chunk.min,max:chunk.max});
  10.                 if (ds.numObjects ==0)
  11.                   zeroChunk++;
  12.                 else {
  13.                   if (ds.size > chunkSize*1024 *1024) color = "red"; else color = "black";
  14.           print("Chunk: "+chunk._id +" has a size of "+colorize(ds.size,color)+", and includes "+ds.numObjects+" objects (took "+ds.millis+"ms)")
  15.                  }
  16.     }
  17. )
  18. print(zeroChunk*100/db.getSiblingDB("config").chunks.find({ns : ns}).count() + " chunks were empty");
clone this paste RAW Paste Data