关于mongodb的db.stats() 的解释:
其中:
collections: 集合的个数
objects: 对象的个数, 循环每个集合得到的,每个集合的记录数(nrecords)之和
avgObjSize: 平均每个对象的大小, 通过 dataSize / objects 得到
dataSize: 每个集合的dataSize之和
storageSize: 每个集合的storageSize之和
fileSize: db下面的物理存储文件的大小; 物理存储文件比实际的数据要大一些,这是是mongo的预分配机制
这里最难理解的大概就是 storageSize了,它和dataSize的差异参看: http://blog.nosqlfan.com/html/2654.html
相关代码db\dbcommands.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
bool run(const string& dbname, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool fromRepl ) { list<string> collections; Database* d = cc().database(); if ( d ) d->namespaceIndex.getNamespaces( collections ); longlong ncollections = 0; longlong objects = 0; longlong size = 0; longlong storageSize = 0; longlong numExtents = 0; longlong indexes = 0; longlong indexSize = 0; for (list<string>::const_iterator it = collections.begin(); it != collections.end(); ++it) { const string ns = *it; NamespaceDetails * nsd = nsdetails( ns.c_str() ); if ( ! nsd ) { errmsg = "missing ns: "; errmsg += ns; returnfalse; } ncollections += 1; objects += nsd->stats.nrecords; size += nsd->stats.datasize; int temp; storageSize += nsd->storageSize( &temp ); numExtents += temp; indexes += nsd->nIndexes; indexSize += getIndexSizeForCollection(dbname, ns); } result.append ( "db" , dbname ); result.appendNumber( "collections" , ncollections ); result.appendNumber( "objects" , objects ); result.append ( "avgObjSize" , objects == 0 ? 0 : double(size) / double(objects) ); result.appendNumber( "dataSize" , size ); result.appendNumber( "storageSize" , storageSize); result.appendNumber( "numExtents" , numExtents ); result.appendNumber( "indexes" , indexes ); result.appendNumber( "indexSize" , indexSize ); result.appendNumber( "fileSize" , d->fileSize() ); returntrue; } dDBStats; |
1 2 3 4 5 6 |
longlong Database::fileSize() const { longlong size=0; for (int n=0; exists(n); n++) size += boost::filesystem::file_size( fileName(n) ); return size; } |
1 2 3 4 5 6 7 8 9 10 |
boost::filesystem::path Database::fileName( int n ) const { stringstream ss; ss << name << '.' << n; boost::filesystem::path fullName; fullName = boost::filesystem::path(path); if ( directoryperdb ) fullName /= name; fullName /= ss.str(); return fullName; } |