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

min() / max() with descending order sort not working

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.3, 2.4.7
    • Component/s: Querying
    • ALL

      Setting the .min() or .max() explicitly on a find and using a descending sort seems not to work.

      > db.docs.drop()
      > for (var i=0; i<10; i++) { db.docs.insert({number: i}) }
      > db.docs.ensureIndex({number: 1})
      

      no sort

      > db.docs.find().max({number: 4})
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e3"), "number" : 0 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e4"), "number" : 1 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e5"), "number" : 2 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e6"), "number" : 3 }
      

      ascending sort

      > db.docs.find().max({number: 4}).sort({number: 1})
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e3"), "number" : 0 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e4"), "number" : 1 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e5"), "number" : 2 }
      { "_id" : ObjectId("51834d678d284e5c5e3ea7e6"), "number" : 3 }
      

      descending sort

      > db.docs.find().max({number: 4}).sort({number: -1})
      > 
      

      I would expect to get the 4 documents back in the 'reverse order' case, in number order: 3, 2, 1, 0. Instead, I get 0 documents back.

      explain of ascending sort

      > db.docs.find().max({number: 4}).sort({number: 1}).explain()
      {
      	"cursor" : "BtreeCursor number_1",
      	"isMultiKey" : false,
      	"n" : 4,
      	"nscannedObjects" : 4,
      	"nscanned" : 4,
      	"nscannedObjectsAllPlans" : 4,
      	"nscannedAllPlans" : 4,
      	"scanAndOrder" : false,
      	"indexOnly" : false,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"start" : {
      			"number" : {
      				"$minElement" : 1
      			}
      		},
      		"end" : {
      			"number" : 4
      		}
      	},
      	"server" : "capslock.local:27017"
      }
      

      explain of descending sort

      > db.docs.find().max({number: 4}).sort({number: -1}).explain()
      {
      	"cursor" : "BtreeCursor number_1 reverse",
      	"isMultiKey" : false,
      	"n" : 0,
      	"nscannedObjects" : 0,
      	"nscanned" : 0,
      	"nscannedObjectsAllPlans" : 0,
      	"nscannedAllPlans" : 0,
      	"scanAndOrder" : false,
      	"indexOnly" : false,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"start" : {
      			"number" : {
      				"$minElement" : 1
      			}
      		},
      		"end" : {
      			"number" : 4
      		}
      	},
      	"server" : "capslock.local:27017"
      }
      

            Assignee:
            ian@mongodb.com Ian Whalen (Inactive)
            Reporter:
            thomas.rueckstiess@mongodb.com Thomas Rueckstiess
            Votes:
            3 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: