'what is a collection scan in mongodb?

I'm aware what a "full collection scan" is. But i'm a little unsure if the term "collection scan" applies to queries that use B-tree cursor. Do queries that use a cursor other than the basic cursor perform a collection scan?



Solution 1:[1]

A collection scan is, well, literally scanning the whole collection. This happens when the user requests to find documents using some conditions that do cannot be answered using an index. For example lets say, we have a users collection with fields such as name, age, hair color, address, phone number and country

user = {"name" : "ABC",
         "age" : 25,
         "hair color" : "brown",
         "address" : "XYZ",
         "phone number" : 1234567890,
         "country" :"Canada"
       }

Further if we have an index on name and query the DB using,

 db.users.find({"name" : "ABC"});

Here since we have an index on the name field the query optimizer would use the index as a performance optimizing approach.

Suppose you query the DB for some other field. Lets say, address

db.users.find({"address" : "XYZ"});

Here the query optimizer would love to shorten its response time for the query, but since it has no prior information about the records in the collection, it has to go through each and every document in the collection to see if that document's address field matched the one in the query. If it does then we return that document. I am sure you know this is where the index comes in since it maintains pointers by "grouping" docs according to certain criteria.

For more info, you can look here.

For your question, a query that uses a B-tree cursor, does it to avoid performing a collection scan and hence queries using any kind of cursor other than the basic cursor "mostly" avoid a collection scan.

You can force it perform a collection scan even if theres exists an index on the field that is being queried. You can read about it here

Solution 2:[2]

Collection Scan: performed when querying one or more fields that do not have an index, so in that case, all documents of the same collection will be scanned one by one to find all documents that satisfy the condition.

in that case, the cost will be O(N) where N is the number of documents in the scanned collection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Works On Mine
Solution 2 Ahmad Al-Kurdi