[SERVER-10636] Unexpected results for $near with 2d index and GeoJSON point Created: 28/Aug/13  Updated: 11/Jul/16  Resolved: 31/Oct/13

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 2.5.2
Fix Version/s: 2.5.4

Type: Bug Priority: Minor - P4
Reporter: A. Jesse Jiryu Davis Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

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?



 Comments   
Comment by auto [ 31/Oct/13 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: SERVER-10636 Verify that GeoJSON points are prohibited with 2d index and $near

Signed-off-by: Matt Kangas <matt.kangas@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/7efb2853b2b50225c748aa38a8c48a4ee7d47805

Comment by A. Jesse Jiryu Davis [ 31/Oct/13 ]

Recent work in the query engine has fixed this. Querying a 2d index with a GeoJSON point now yields:

error: {
	"$err" : "Couldn't get runner for query because: No query solutions query is
ns=test.geo_2d_with_geojson_point limit=1 skip=0\nTree: GEONEAR  field=loc
maxdist=1.79769e+308 isNearSphere=0\n\nSort: {}\nProj: {}\n",
	"code" : 17007
}

Comment by Daniel Pasette (Inactive) [ 29/Aug/13 ]

I think the behavior should be to uassert.

Comment by A. Jesse Jiryu Davis [ 28/Aug/13 ]

FYI hari.khalsa@10gen.com.

Generated at Thu Feb 08 03:23:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.