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

$geoWithin with backwards $box coordinates erroneously returns results

      // Setup
      > t = db.t
      > t.drop()
      > t.ensureIndex({loc: "2d"})
      > t.save({loc: [0, 0]})
      
      // The doc is in the box, but the box coordinates are backwards. Should return zero results.
      > t.find({loc: {$geoWithin: {$box: [ [ 5, 5 ], [-5, -5] ] }}})
      { "_id" : ObjectId("534ffa11a7931e19d3eb0027"), "loc" : [ 0, 0 ] } // Got a result, but we shouldn't have.
      
      // If we hint to use a non-2d index, then we correctly return zero results:
      > t.find({loc: {$geoWithin: {$box: [ [ 5, 5 ], [-5, -5] ] }}}).hint({_id: 1})
      
      // Everything works as expected if we flip the $box coordinates:
      > t.find({loc: {$geoWithin: {$box: [ [ -5, -5 ], [5, 5] ] }}})
      { "_id" : ObjectId("534ffa11a7931e19d3eb0027"), "loc" : [ 0, 0 ] }
      > t.find({loc: {$geoWithin: {$box: [ [ -5, -5 ], [5, 5] ] }}}).hint({_id: 1})
      { "_id" : ObjectId("534ffa11a7931e19d3eb0027"), "loc" : [ 0, 0 ] }
      

      Original Description

      if I exec this query:

      db.Listings.find( { location : { $geoWithin : { $box :
             [ [ -80.84271171946818, 26.723380302236098 ],
               [ -82.32517937083537, 26.21012478434639 ] ] } } } ).explain()
      

      I get "n" : 49097 so there are around 50K records

      (this is a cluster with 2 shards and shard key is the hashed _id )

      but when I put the .sort({_id: -1}) the result is "n": 0 - even if I change the sort to another field (indexed field) I get the same result

      MongoDB 2.6.0 (all machines mogos, conf boxes and shards)

            Assignee:
            siyuan.zhou@mongodb.com Siyuan Zhou
            Reporter:
            eriol Eriol Gjergji
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: