Show
In MongoDB 3.3.5 shell using the WiredTiger storage engine:
use TESTMAXSCAN
db.users.insert({name:"sue",age:26,status:"A"})
db.users.insert({name:"bob",age:42,status:"B"})
db.users.insert({name:"mark",age:29,status:"B"})
db.users.find()
{ "_id" : ObjectId("57891a9c52fec6668177681b"), "name" : "sue", "age" : 26, "status" : "A" }
{ "_id" : ObjectId("57891aad52fec6668177681c"), "name" : "bob", "age" : 42, "status" : "B" }
{ "_id" : ObjectId("57891abc52fec6668177681d"), "name" : "mark", "age" : 29, "status" : "B" }
// This is good (a document scan):
db.users.find().maxScan(2)
{ "_id" : ObjectId("57891a9c52fec6668177681b"), "name" : "sue", "age" : 26, "status" : "A" }
{ "_id" : ObjectId("57891aad52fec6668177681c"), "name" : "bob", "age" : 42, "status" : "B" }
db.users.createIndex({name:1})
// This is where I am confused with an index scan -- why only one?
db.users.find().maxScan(2).sort({name:1})
{ "_id" : ObjectId("57891aad52fec6668177681c"), "name" : "bob", "age" : 42, "status" : "B" }
In MongoDB 2.2.0 shell using the legacy database:
use TESTMAXSCAN
db.users.insert({name:"sue",age:26,status:"A"})
db.users.insert({name:"bob",age:42,status:"B"})
db.users.insert({name:"mark",age:29,status:"B"})
db.users.find()
{ "_id" : ObjectId("57891a9c52fec6668177681b"), "name" : "sue", "age" : 26, "status" : "A" }
{ "_id" : ObjectId("57891aad52fec6668177681c"), "name" : "bob", "age" : 42, "status" : "B" }
{ "_id" : ObjectId("57891abc52fec6668177681d"), "name" : "mark", "age" : 29, "status" : "B" }
db.users.find({$query:{},$maxScan:2})
{ "_id" : ObjectId("57891c05adc58efb84db213f"), "name" : "sue", "age" : 26, "status" : "A" }
{ "_id" : ObjectId("57891c06adc58efb84db2140"), "name" : "bob", "age" : 42, "status" : "B" }
db.users.createIndex({name:1})
// Notice it prints two documents
db.users.find({$query:{},$maxScan:2,$orderby:{name:1}})
{ "_id" : ObjectId("57891c06adc58efb84db2140"), "name" : "bob", "age" : 42, "status" : "B" }
{ "_id" : ObjectId("57891c09adc58efb84db2141"), "name" : "mark", "age" : 29, "status" : "B" }