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

MaxKey behaves in an unexpected way

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.2.0
    • JavaScript, Querying
    • None
    • ALL

    Description

      Problem:

      MaxKey seems to produce an incorrect match, resulting in returning too many documents.

      The explain seems to use the right index, not sure if the evaluation of MaxKey is correct

      {
      	"cursor" : "BtreeCursor fields_1",
      	"isMultiKey" : true,
      	"n" : 3,
      	"nscannedObjects" : 5,
      	"nscanned" : 5,
      	"nscannedObjectsAllPlans" : 5,
      	"nscannedAllPlans" : 5,
      	"scanAndOrder" : false,
      	"indexOnly" : false,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"fields" : [
      			[
      				{
      					"key1" : 2
      				},
      				{
      					"key1" : { $maxKey : 1 }
      				}
      			]
      		]
      	},
      	"server" : "vero:27017"
      }
      

      Reproduce:

      db.form.drop();
       
      db.form.insert({"fields" : [ { "key1" : 2 }, { "key2" : "Worlds" } ] })
      db.form.insert({"fields" : [ { "key2" : "!" }, { "key5" : "text" } ] })
      db.form.insert({"fields" : [ { "key1" : 1 }, { "key2" : "Hello" } ] })
       
      db.form.ensureIndex({fields:1})
       
      > db.form.find({"fields": {$elemMatch: { $gte: {key1:2}, $lt: {key1:MaxKey}}}})
      { "_id" : ObjectId("509c19a9b72f2fa3103bd760"), "fields" : [ { "key1" : 2 }, { "key2" : "Worlds" } ] }
      { "_id" : ObjectId("509c19a9b72f2fa3103bd761"), "fields" : [ { "key2" : "!" }, { "key5" : "text" } ] }
      { "_id" : ObjectId("509c19a9b72f2fa3103bd762"), "fields" : [ { "key1" : 1 }, { "key2" : "Hello" } ] }
      
      

      This should return only a single object

      { "_id" : ObjectId("509c19a9b72f2fa3103bd760"), "fields" : [ { "key1" : 2 }, { "key2" : "Worlds" } ] }
      

      Attachments

        Activity

          People

            david.storch@mongodb.com David Storch
            alvin Alvin Richards (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: