Clement Amarnath
Clement Amarnath

Reputation: 5476

Total objects doesnot match with sum of individual document collection counts in mongodb, the collections count is also mismatch

I get the following when I execute the db.stats in my test database

db.stats()
{
        "db" : "test",
        "collections" : 6,
        "objects" : 1000032,
        "avgObjSize" : 112.00064797926467,
        "dataSize" : 112004232,
        "storageSize" : 175812608,
        "numExtents" : 17,
        "indexes" : 8,
        "indexSize" : 169905456,
        "fileSize" : 2080374784,
        "nsSizeMB" : 16,
        "extentFreeList" : {
                "num" : 77,
                "totalSize" : 760766464
        },
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 22
        },
        "ok" : 1
}

These are the different collections which I have in the test database

db.getCollectionNames();
[
        "foo",
        "sentences",
        "stackoverflow",
        "system.indexes",
        "system.profile"
]

Here is the seperate count on documents on each collection

db.foo.count() - 1000000
db.sentences.count() - 7
db.stackoverflow.count() - 2
db.system.indexes.count() - 8
db.system.profile.count() - 2

Now I have two questions

1) Why I'm getting the collections as 6 in my db.stats() query, when I only have 5 in db.getCollectionNames()

2) The total number of documents should be 1000000+7+2+8+2 = 1000016, but in the db.stats, I'm getting "objects" : 1000032

Please help with your thoughts on understanding this

db.version();
3.0.3

db.serverStatus().storageEngine
{ "name" : "mmapv1" }

Additional Information

I created a brand new DB in mongo shell and executed db.stats and get the following results

use newdb
db.stats()
{
        "db" : "newdb",
        "collections" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}

Now all the data was showing correctly then I created a collection and inserted only one record, then again I got the discrepancy in the count.

db.mycollection.insert({_id:1, name:'Clement Amarnath'});

WriteResult({ "nInserted" : 1 })

db.stats()
{
        "db" : "newdb",
      "collections" : 3,
      "objects" : 5,
        "avgObjSize" : 60.8,
        "dataSize" : 304,
        "storageSize" : 20480,
        "numExtents" : 3,
        "indexes" : 1,
        "indexSize" : 8176,
        "fileSize" : 67108864,
        "nsSizeMB" : 16,
        "extentFreeList" : {
                "num" : 0,
                "totalSize" : 0
        },
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 22
        },
        "ok" : 1
}

db.getCollectionNames()
[ "mycollection", "system.indexes" ]

db.system.indexes.count()
1

Upvotes: 0

Views: 99

Answers (1)

Clement Amarnath
Clement Amarnath

Reputation: 5476

The reason for collection count is always +1 than the actual collections listed on db.getCollectionNames, is there a hidden collection system.namespaces which stores the information about collections.

This also solves the difference in the total objects.

db.stats()
{
        "db" : "test",
      "collections" : 6, 
      "objects" : 1000035,
        "avgObjSize" : 112.0006479773208,
        "dataSize" : 112004568,
        "storageSize" : 175812608,
        "numExtents" : 17,
        "indexes" : 8,
        "indexSize" : 169905456,
        "fileSize" : 2080374784,
        "nsSizeMB" : 16,
        "extentFreeList" : {
                "num" : 77,
                "totalSize" : 760766464
        },
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 22
        },
        "ok" : 1
}

Collections

db.getCollectionNames()
[
        "foo",
        "sentences",
        "stackoverflow",
        "system.indexes",
        "system.profile"
]

Collections Count

Number of Collections listed in db.stats is 6 = db.getCollectionNames() is 5 + the collection system.namespaces = gives the collection count as 6.

Objects Count

db.stackoverflow.count() - 5
db.foo.count() - 1000000
db.system.namespaces.count() - 13
db.system.indexes.count() - 8
db.system.profile.count() - 2
db.sentences.count() - 7
db.stackoverflow.count() - 5

Document count = 5+1000000+13+8+2+7+5 = 10000035

Upvotes: 1

Related Questions