using db.stats() to return selected fields

53 views
Skip to first unread message

Tony C

unread,
Feb 13, 2015, 5:14:29 PM2/13/15
to mongod...@googlegroups.com

Is there a way to use db.collection.stats() to return only 4 selected columns (ns, count, size, storageSize)  in a single output line?


> db.author.stats().ns;  <-- this returns the 1 feild

> db.author.stats();  <-- this returns all
{
        "ns" : "cse.author",
        "count" : 376,
        "size" : 624768,
        "avgObjSize" : 1661,
        "storageSize" : 696320,
        "numExtents" : 4,
        "nindexes" : 4,
        "lastExtentSize" : 524288,
        "paddingFactor" : 1,
        "systemFlags" : 1,
        "userFlags" : 1,
        "totalIndexSize" : 122640,
        "indexSizes" : {
                "_id_" : 24528,
                "last_name_1" : 24528,
                "events.date_1" : 24528,
                "first_name_text_last_name_text" : 49056
        },
        "ok" : 1

I'm not sure the syntax of getting multiple selected fields.  I appreciate any assistance.
Thanks,
Tony

Will Berkeley

unread,
Feb 14, 2015, 12:18:09 PM2/14/15
to mongod...@googlegroups.com
Make a helper function around db.stats() so it outputs just the fields you want, e.g.

> var myStats = function(collName) {
    var stats = db[collName].stats()
    print(stats.ns, stats.count, stats.size, stats.storageSize)
}

> db.test.drop()
> db.test.insert({ "a" : 1 })
> myStats("test")
test.test 1 48 8192

-Will

Tony C

unread,
Feb 16, 2015, 4:18:39 PM2/16/15
to mongod...@googlegroups.com
That's great, thanks Will.  If I wanted to make "test" a variable so it would loop through all collections in the DB is that possible.  I know this might be basic, but  I'm new to JSON,  

Dhananjay D. Makwana

unread,
Feb 16, 2015, 4:26:14 PM2/16/15
to mongod...@googlegroups.com
You can get them using
http://docs.mongodb.org/manual/reference/method/db.getCollectionNames/#db.getCollectionNames
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/530267a3-e217-4c6e-a2bf-7084b1f609a9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


-DJ
609-309-5340

Asya Kamsky

unread,
Feb 18, 2015, 10:48:07 AM2/18/15
to mongod...@googlegroups.com
I like to see that sort of information also (since db.printCollectionStats() outputs JSON which is harder to read.

I've got some examples here:  
The last function in particular.

Asya
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/530267a3-e217-4c6e-a2bf-7084b1f609a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
{ "name" : "Asya Kamsky",
  "place" : [ "New York", "Palo Alto", "Everywhere else" ],
  "email" : "as...@mongodb.com",
  "blog" : "http://www.askasya.com/",
  "twitter": "@asya999" }
Reply all
Reply to author
Forward
0 new messages