Why doesn't mongo count add up?

Does anyone know why mongodb is returning a weird result when doing the next 3 queries? It seems that the result of the first and second counts should be equal to the third, but it is not. All I do is count the number of documents containing a specific field + those that do not contain that field. I expect this to be equal to the total number of documents:

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

      

+3


source to share


1 answer


Could it be because you have a sparse index on field f1? If so, 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()

      

+2


source







All Articles