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

Unexpected results for $near with 2d index and GeoJSON point

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.5.4
    • Affects Version/s: 2.5.2
    • Component/s: Geo
    • Labels:
      None
    • ALL

      Behavior of $near with a 2d index and a GeoJSON point has some surprises:

      > db.test.createIndex({loc:'2d'});
      > db.test.insert({_id: 1, loc:[0, 0]});
      > // point [1, 0] is farther than 0 units from origin as expected
      > printjson(db.test.findOne({
      ...     loc:{
      ...         $near:[1, 0],
      ...         $maxDistance: 0}}));
      null
      > // but GeoJSON point [1, 0] is 0 units from origin??
      > printjson(db.test.findOne({
      ...     loc:{
      ...         $near:{
      ...             $geometry:{
      ...                 type:'Point',
      ...                 coordinates:[1, 0]}},
      ...         $maxDistance: 0}}));
      { "_id" : 1, "loc" : [ 0, 0 ] }
      > // Wrong sort order:
      > db.test.insert({_id: 2, loc:[1, 0]});
      > db.test.find({
      ...     loc:{
      ...         $near:{
      ...             $geometry:{
      ...                 type:'Point',
      ...                 coordinates:[2, 0]}}}});
      { "_id" : 1, "loc" : [  0,  0 ] }
      { "_id" : 2, "loc" : [  1,  0 ] }
      > // GeoJSON point [2, 0] is 1 unit from [1, 0], but 0 units from origin?
      > db.test.find({
      ...     loc:{
      ...         $near:{
      ...             $geometry:{
      ...                 type:'Point',
      ...                 coordinates:[2, 0]}},
      ...         $maxDistance: 1}});
      { "_id" : 1, "loc" : [  0,  0 ] }
      { "_id" : 2, "loc" : [  1,  0 ] }
      >
      > db.test.find({
      ...     loc:{
      ...         $near:{
      ...             $geometry:{
      ...                 type:'Point',
      ...                 coordinates:[2, 0]}},
      ...         $maxDistance:.999}});
      { "_id" : 1, "loc" : [  0,  0 ] }
      

      The documentation says "for GeoJSON points, use a 2dsphere index": should we uassert that GeoJSON points aren't used with 2d index, or fix the behavior?

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: