Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB's count() incorrectly returns 0 if a query is given

Our database is currently running and everything seems to fine. I wanted to do some statistics so I did some standard calls. Basically I wanted the number of some specific data.

First some basic calls to show you that there actually is data in the database.

> db.files.count()
814639

> db.files.find({"migrated":true})
{ "migrated" : true, "filename" : "bleh",... }
...

So clearly there's data, and the call returns it. Now I want to find out how many results there is, but I get this:

> db.files.count({"migrated":true})        
0

And I did this too:

> db.files.find({"migrated":true}).count()
0

Is there anyone who has any idea why this could be happening?

The versions is:

> db.version()
1.8.1

Any help would be much appreciated

like image 727
freeall Avatar asked Nov 04 '22 21:11

freeall


2 Answers

It's probable that this question is related to the following bugs:

  • Interrupted count query returns 0 as count result
  • count command returns zero in some error cases rather than reporting an error with ok:false

In my case (mongodb 2.0.1) was related to database corruption. See find().count() bug?

like image 100
sw. Avatar answered Nov 09 '22 14:11

sw.


Been a while, but I'm closing this now. But it was a corrupt database. Had to manually move all the items from one database to a new one since a backup stopped when it found the corrupt data, and valid data afterwards wasn't being backed up.

like image 28
freeall Avatar answered Nov 09 '22 12:11

freeall