[SERVER-42097] .count() returns wrong value Created: 07/Jul/19  Updated: 08/Jul/19  Resolved: 08/Jul/19

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

Type: Question Priority: Major - P3
Reporter: Yura Inversion Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

While debugging a problem in an app I found that it is caused by a wrong value returned from Mongo. So I end up have a collection for which .count() always returns 136 while the collection is empty.

Here is what I see in the mongo terminal:

 

> db.ReadStats.findOne() — normal collection
null
> db.ReadStats.count()
0
> db.PendingRecords.findOne() — broken collection?
null
> db.PendingRecords.count()
136

 

When I do dump — this collection is saved as PendingRecords.bson 0 B file. So it is really empty. And according to our log of logic actions, it really should be empty.

> db.repairDatabase()
{ "ok" : 1 }
> db.PendingRecords.count()
0

Repair helped, but what could go wrong? What should we do to do not encounter this again? Because it just breaks our app (



 Comments   
Comment by Yura Inversion [ 07/Jul/19 ]

Thank you for the information — now I understand the issue.

It is really an old version.

Comment by Asya Kamsky [ 07/Jul/19 ]

Are you really on 3.2.22? That's a very old version. In more recent versions you can use db.coll.countDocuments() to get a real count even without a predicate.

Comment by Daniel Pasette (Inactive) [ 07/Jul/19 ]

Hi Yuba, looks like you found the issue – count without a query predicate ("fast count") can drift after an unclean shutdown.

See this documentation on how to correct the issue.

Comment by Yura Inversion [ 07/Jul/19 ]

I remember that this instance of mongod recently crashed once during active processing. So probably this can be a reason.

In a log I found that moment — there was no exit signal

2019-07-05T14:52:28.570+0300 I NETWORK [initandlisten] connection accepted from 127.0.0.1:42760 #31 (17 connections now open)
2019-07-05T14:52:28.607+0300 I NETWORK [initandlisten] connection accepted from 127.0.0.1:42774 #32 (18 connections now open)
2019-07-05T14:52:29.405+0300 I NETWORK [conn29] end connection 127.0.0.1:42688 (17 connections now open)
2019-07-05T14:52:29.406+0300 I NETWORK [conn30] end connection 127.0.0.1:42692 (16 connections now open)
2019-07-05T14:52:29.406+0300 I NETWORK [conn31] end connection 127.0.0.1:42760 (15 connections now open)
2019-07-05T14:52:29.406+0300 I NETWORK [conn32] end connection 127.0.0.1:42774 (14 connections now open)
2019-07-05T14:56:54.750+0300 I CONTROL [main] ***** SERVER RESTARTED *****

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