[SERVER-20687] $geoWithin queries not hitting 2dsphere index Created: 29/Sep/15  Updated: 08/Oct/15  Resolved: 08/Oct/15

Status: Closed
Project: Core Server
Component/s: Geo, Index Maintenance
Affects Version/s: 3.0.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrew Kouri Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Create a collection from MapReduce query that contains geoJson points in the value.location field. Add a 2dsphere index to that embedded field. Run a $geoWithin query such as:

"value.avg_location" : {
"$geoWithin" :

{ "$polygon" : [ [ -122.07496047019957, 37.65882781135178 ], [ -122.07496047019957, 37.663906822384874 ], [ -122.0871913433075, 37.663906822384874 ], [ -122.0871913433075, 37.65882781135178 ], [ -122.07496047019957, 37.65882781135178 ] ] }

}
},

Participants:

 Description   

When I run a query on an indexed location field (which contains geoJSON points) I notice that the query is doing a collection scan. Deleting the index and running the query takes about 5 minutes. After adding the index back again, the query still takes about 5 minutes. Here are the steps I've taken to debug:

http://www.andrewkouri.com/debugging-mongodbs-2dsphere-geoindex/



 Comments   
Comment by Wan Bachtiar [ 08/Oct/15 ]

Hi Andrew,

Thanks for the report.

As noted in MongoDB documentation, only the 2d geospatial index supports the $polygon operator : http://docs.mongodb.org/manual/reference/operator/query/polygon/#behavior

Please use the $geometry operator to specify a polygon shape in GeoJSON format, if the index is 2dsphere.

Regards,

Wan

Comment by Andrew Kouri [ 29/Sep/15 ]

Update: It appears that the index miss only happens for the "old" type of geo queries:

{
    "location": {
        "$geoWithin": {
            "$polygon": [
                [-122.07496047019957,
                    37.65882781135178
                ],
                [-122.07496047019957,
                    37.663906822384874
                ],
                [-122.0871913433075,
                    37.663906822384874
                ],
                [-122.0871913433075,
                    37.65882781135178
                ],
                [-122.07496047019957,
                    37.65882781135178
                ]
            ]
        }
    }
}

The following uses an "IXSCAN":

{
   "location": {
      $geoWithin: {
         $geometry: {
            type: "Polygon",
            coordinates: [  [
              [-122.07496047019957, 37.65882781135178], [-122.07496047019957, 37.663906822384874],[-122.0871913433075, 37.663906822384874],[-122.0871913433075, 37.65882781135178], [-122.07496047019957, 37.65882781135178]
            ] ]
         }
      }
   }
}

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