[SERVER-26535] result of collection count function does not equals with result of aggregate count function Created: 09/Oct/16  Updated: 03/Apr/23  Resolved: 11/Oct/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: jacky chen Assignee: Kelsey Schubert
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

db.version() = 3.2.8
db.serverBuildInfo() as below

{
"allocator" : "tcmalloc",
"bits" : 64,
"buildEnvironment" :

{ "cc" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64", "ccflags" : "/nologo /EHsc /W3 /wd4355 /wd4800 /wd4267 /wd4244 /wd4290 /wd4068 /wd4351 /we4013 /we4099 /we4930 /Z7 /errorReport:none /MD /O2 /Oy- /Gw /Gy /Zc:inline", "cxx" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64", "cxxflags" : "/TP", "distarch" : "x86_64", "distmod" : "2008plus-ssl", "linkflags" : "/nologo /DEBUG /INCREMENTAL:NO /LARGEADDRESSAWARE /OPT:REF", "target_arch" : "x86_64", "target_os" : "windows" }

,
"debug" : false,
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0",
"javascriptEngine" : "mozjs",
"maxBsonObjectSize" : 16777216,
"modules" : [ ],
"ok" : 1,
"openssl" :

{ "compiled" : "OpenSSL 1.0.1p-fips 9 Jul 2015", "running" : "OpenSSL 1.0.1p-fips 9 Jul 2015" }

,
"storageEngines" : [
"devnull",
"ephemeralForTest",
"mmapv1",
"wiredTiger"
],
"sysInfo" : "deprecated",
"targetMinOS" : "Windows 7/Windows Server 2008 R2",
"version" : "3.2.8",
"versionArray" : [
3,
2,
8,
0
]
}


Operating System: ALL
Steps To Reproduce:

db.collection.count() shows 5
but aggregate count as below shows 6

db.runCommand(
  {
      aggregate:"collection", 
      pipeline:[
             {
                 $group:{ _id:null, count: {$sum:1} }
             }
      ] 
   }
)

Participants:

 Description   

evn description:
standalone database.
6 document data exists in collection.

but we got two different results via pure count function and aggregate count function according to below docs.
https://docs.mongodb.com/manual/reference/command/count/#dbcmd.count

I'm not sure whether this is a bug or my command is wrong.



 Comments   
Comment by Kelsey Schubert [ 11/Oct/16 ]

Hi update0412,

I'm glad that running validate resolved this issue. Following an unclean shutdown, it is expected that these counts may be off by some amount. The validate command steps through the documents and indexes and performs a number of checks on them. As part of this process, the recordstore count is updated.

Feel free to take a look at the relevant source code for additional information:
https://github.com/mongodb/mongo/blob/r3.2.10/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp#L1550
https://github.com/mongodb/mongo/blob/r3.2.10/src/mongo/db/catalog/collection.cpp#L963
https://github.com/mongodb/mongo/blob/r3.2.10/src/mongo/db/exec/count.cpp#L75

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-users group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Kind regards,
Thomas

Comment by jacky chen [ 11/Oct/16 ]

Hi Thomas,

Thanks for your quick reply.

I try to execute db.system.users.validate(true) and then count() returns correct value.

below is validate print

> db.system.users.validate(true)
{
        "ns" : "admin.system.users",
        "nrecords" : 6,
        "nIndexes" : 2,
        "keysPerIndex" : {
                "admin.system.users.$_id_" : 6,
                "admin.system.users.$user_1_db_1" : 6
        },
        "indexDetails" : {
                "admin.system.users.$_id_" : {
                        "valid" : true
                },
                "admin.system.users.$user_1_db_1" : {
                        "valid" : true
                }
        },
        "valid" : true,
        "errors" : [ ],
        "ok" : 1
}

Could you share more detials about the validate functionality ?
since only system.users and system.version happen this problem, counting collectoins created by me is correct.

thanks a lot.

B.R.
Jacky

Comment by Kelsey Schubert [ 10/Oct/16 ]

Hi update0412,

Thank you for reporting this behavior. Would you please provide the output of db.system.users.validate()? And confirm if the issue is resolved?

Please be aware that validate is resource intensive and may have an impact on the performance of your MongoDB instance.

Thank you,
Thomas

Comment by jacky chen [ 09/Oct/16 ]

collection is admin.system.users

Generated at Thu Feb 08 04:12:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.