Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the index size with pymongo?

With monogdb, I can run db.collection.stats() to find the size, in bytes, for each index.

PyMongo seems to be missing this operation.

Is there a way to find this information with PyMongo?

like image 437
Ray Toal Avatar asked Jan 15 '23 03:01

Ray Toal


1 Answers

import pymongo
connect = pymongo.Connection('mongodb://localhost', safe=True)
db = connect.test
db.command('collStats', 'collection')

Result:

{
  u'count': 2,
  u'ns': u'test.test2',
  u'ok': 1.0,
  u'lastExtentSize': 8192,
  u'avgObjSize': 94.0,
  u'totalIndexSize': 8176,
  u'systemFlags': 1,
  u'userFlags': 0,
  u'numExtents': 1,
  u'nindexes': 1,
  u'storageSize': 8192,
  u'indexSizes': {u'_id_': 8176},
  u'paddingFactor': 1.0,
  u'size': 188
}

P.S. test2 in the result is my collection name

  • http://api.mongodb.org/python/current/api/pymongo/database.html
  • http://docs.mongodb.org/manual/reference/collection-statistics/
like image 60
Chien-Wei Huang Avatar answered Jan 26 '23 00:01

Chien-Wei Huang