Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of collections in mongodb

Tags:

mongodb

I have a mongodb with number of collections.
Eg:

--mongo_db (db name)

-- aa
-- bb
-- cc (collections)

Need number of collections in mongo_db. In the above case result 3

How can i get???

like image 300
SuFi Avatar asked Nov 18 '16 07:11

SuFi


2 Answers

in mongo shell you could use db.getCollectionNames().length

like image 184
djaszczurowski Avatar answered Oct 18 '22 00:10

djaszczurowski


I will use db.stats() which as specified in the documentation:

Returns statistics that reflect the use state of a single database.

Demo:

> show collections
coll
collection
foo
mumbai
spam
> db.stats()
{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 14,
        "avgObjSize" : 154.64285714285714,
        "dataSize" : 2165,
        "storageSize" : 98304,
        "numExtents" : 0,
        "indexes" : 5,
        "indexSize" : 98304,
        "ok" : 1
}
> db.stats().collections
5
like image 40
styvane Avatar answered Oct 17 '22 23:10

styvane