Sagi Mann
Sagi Mann

Reputation: 3610

why mongo count doesn't add up?

Does anyone know why mongodb returns strange result count when performing the next 3 queries? It seems that the result of the first and second counts should be equal to the third, but it does not. All I do is count the number of docs that contain a certain field + the ones that do not contain that field.. I expect this to be equal to the total number of docs:

> db.col1.find({f1:{$exists:false}}).count()
0
> db.col1.find({f1:{$exists:true}}).count()
267837
> db.col1.count()
268185

Upvotes: 3

Views: 267

Answers (1)

sergiuz
sergiuz

Reputation: 5529

Maybe because you have a sparse index on f1 field? If that is true, try this and see if they match:

db.col1.find({f1:{$exists:false}}).hint({f1:1}).count()
db.col1.find({f1:{$exists:false}}).hint({_id:1}).count()

Upvotes: 2

Related Questions