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

Geospatial result paging fails when sorting with additional keys

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 1.4.0
    • Querying
    • None
    • Linux 32 bit

    Description

      Using limit() and skip() to page result sets yields unexpected results when utilizing geospatial querying and an additional sort key. Objects are not correctly sorted per the additional sort key, and some of the same objects appear in subsequent result sets.

      To reproduce:

      use foo
      db.foo.ensureIndex({loc: "2d"})
      db.foo.ensureIndex({random: 1 })
      for( var i = 1; i <= 100; i++ ){db.foo.save( { loc: [1, 1], random: Math.floor(Math.random()*100) } )}
       
      /* query 5 objects per set */
       
      /* page 1 */
      db.foo.find({loc: {$near: [1, 1]}}).limit(5).skip((1 - 1) * 5).sort({random: 1})
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931c"), "loc" : [ 1, 1 ], "random" : 40 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931e"), "loc" : [ 1, 1 ], "random" : 55 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931f"), "loc" : [ 1, 1 ], "random" : 63 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931b"), "loc" : [ 1, 1 ], "random" : 71 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931d"), "loc" : [ 1, 1 ], "random" : 75 }
       
       
      /* page 2 */
      db.foo.find({loc: {$near: [1, 1]}}).limit(5).skip((2 - 1) * 5).sort({random: 1})
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931f"), "loc" : [ 1, 1 ], "random" : 63 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad9324"), "loc" : [ 1, 1 ], "random" : 67 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931b"), "loc" : [ 1, 1 ], "random" : 71 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931d"), "loc" : [ 1, 1 ], "random" : 75 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad9322"), "loc" : [ 1, 1 ], "random" : 99 }
       
      /* page 3 */
      db.foo.find({loc: {$near: [1, 1]}}).limit(5).skip((3 - 1) * 5).sort({random: 1})
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad9324"), "loc" : [ 1, 1 ], "random" : 67 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931b"), "loc" : [ 1, 1 ], "random" : 71 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad931d"), "loc" : [ 1, 1 ], "random" : 75 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad9328"), "loc" : [ 1, 1 ], "random" : 91 }
      { "_id" : ObjectId("4bb2d5e2284b5ab1d9ad9322"), "loc" : [ 1, 1 ], "random" : 99 }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            twhite Timbo White
            Votes:
            13 Vote for this issue
            Watchers:
            13 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: