how to get storageSize from pymongo?

1,048 views
Skip to first unread message

kuer

unread,
Nov 2, 2009, 8:58:51 PM11/2/09
to mongodb-user
Hi, all,

In mongo shell, command "db.mycollection.storageSize()" can get
storage size about mycollection.

How to do that in pymongo ?

thanks

-- kuer

Eliot Horowitz

unread,
Nov 2, 2009, 9:17:41 PM11/2/09
to mongod...@googlegroups.com
All things like this are commands.
This one for example:

from pymongo.connection import Connection
connection = Connection()
db = connection["test"]
print( db._command( { "collstats" : "foo" } ) )

Also, in general you can always do:
> db.mycollection.storageSize
function () {
return this.stats().storageSize;
}
> db.mycollection.stats
function () {
return this._db.runCommand({collstats:this._shortName});

kuer

unread,
Nov 2, 2009, 10:01:44 PM11/2/09
to mongodb-user
Thanks Eliot.

but it does NOT work.

>>> print( db._command( { "collstats" : "foo" } ) )

Traceback (most recent call last):
File "simple_demo.py", line 79, in ?
print( db._command( { "collstats" : "foo" } ) )
File "/home/s/apps/mongodb/lib/python/pymongo/database.py", line
190, in _command
raise OperationFailure("command %r failed: %s" %
pymongo.errors.OperationFailure: command {'collstats': 'foo'} failed:
ns not found

>>> print( db._command( { "collstats" : "storageSize" } ) )

Traceback (most recent call last):
File "simple_demo.py", line 79, in ?
print( db._command( { "collstats" : "storageSize" } ) )
File "/home/s/apps/mongodb/lib/python/pymongo/database.py", line
190, in _command
raise OperationFailure("command %r failed: %s" %
pymongo.errors.OperationFailure: command {'collstats': 'storageSize'}
failed: ns not found

I install pymongo-1.1.

Did I misunderstand it ?

-- kuer

Eliot Horowitz

unread,
Nov 2, 2009, 10:04:57 PM11/2/09
to mongod...@googlegroups.com
Does the collection exist already?
If it doesn't, will return that error.

2009/11/2 kuer <kue...@gmail.com>:

kuer

unread,
Nov 2, 2009, 10:58:20 PM11/2/09
to mongodb-user
Thanks, Eliot.

You are right. I connect the wrong database. It works now.

and, is there a doc/page listing available commands such as
'collstats' ??

thanks

--kuer

On 11月3日, 上午11时04分, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> Does the collection exist already?
> If it doesn't, will return that error.
>
> 2009/11/2 kuer <kuer...@gmail.com>:

Eliot Horowitz

unread,
Nov 2, 2009, 11:12:38 PM11/2/09
to mongod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages