Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-25111

For index scans, maxScan is returning one less than the parameter

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.3.5
    • Component/s: Querying, Shell
    • None
    • Environment:
      Windows 10
      MongoDb 3.3.5
    • Query
    • ALL
    • Hide

      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" }
      
      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" }

      When using maxScan(n) with a sort(), it only returns n-1 documents. When I use $maxScan with MongoDB 2.2.0 with the legacy storage engine, it returns n documents.

      Is this a bug or a change in maxScan that I don't understand?

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            therefore George Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: