How to determine if a full collection scan has been done in MongoDB

indexing, mongodb

Solution

What proportion of difference generally infers a full collection scan - 10%, 20%, 30%+?

This is impossible to say but if it really matters a whole tonne then you could be seeing a performance degradation of up to 200% for an average find; so yes, you WILL notice it. It is much like any other database on this front.

Are there any other ways to check if a full collection scan has been done?

You could start MongoDB with a flag that tells it to never do a full table scan, in which case it will throw an exception when it attempts to: http://docs.mongodb.org/manual/reference/mongod/#cmdoption-mongod--notablescan

However the best way is to just to use `explain` here, you will know when a query does not use an index and is forced to scan the entire collection from either disk or memory.

Problem

I understand that using the output of `.explain()` on a MongoDB query, you can look at the difference between `n` and `nscanned` to determine if a full collection scan has been performed, or if an index has been used. The docs state You want `n` and `nscanned` to be close in value as possible. Kyle Banker's excellent book MongoDB in Action says something very similar: Generally speaking, you want the values of `n` and `nscanned` to be as close together as possible. When doing a collection scan, this is almost never the case. Clearly neither of these statements are definitive about comparing `n` and `nscanned`. What proportion of difference generally infers a full collection scan - 10%, 20%, 30%+? Are there any other ways to check if a full collection scan has been done?

Original source