I have over 600 thousand entries in MongoDb. my user schema is as follows:
{ "_id" : ObjectId, "password" : String, "email" : String, "location" : Object, "followers" : Array, "following" : Array, "dateCreated" : Number, "loginCount" : Number, "settings" : Object, "roles" : Array, "enabled" : Boolean, "name" : Object }
following request:
db.users.find( {}, { name:1, settings:1, email:1, location:1 } ).skip(656784).limit(10).explain()
leads to the following:
{ "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 10, "nscannedObjects" : 656794, "nscanned" : 656794, "nscannedObjectsAllPlans" : 656794, "nscannedAllPlans" : 656794, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 5131, "nChunkSkips" : 0, "millis" : 1106, "server" : "shreyance:27017", "filterSet" : false }
and after deleting the projection query db.users.find().skip(656784).limit(10).explain()
leads to the following:
{ "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 10, "nscannedObjects" : 656794, "nscanned" : 656794, "nscannedObjectsAllPlans" : 656794, "nscannedAllPlans" : 656794, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 5131, "nChunkSkips" : 0, "millis" : 209, "server" : "shreyance:27017", "filterSet" : false }
As far as I know, projection always increases query performance. Therefore, I cannot understand why MongoDB behaves this way. Can someone explain this. And when to use projection, and when not. And how the projection is actually implemented in MongoDB.