OPTIONS

Query a 2d Index

The following sections describe queries supported by the 2d index. For an overview of recommended geospatial queries, see Geospatial Query Compatibility.

Points within a Shape Defined on a Flat Surface

To select all legacy coordinate pairs found within a given shape on a flat surface, use the $geoWithin operator along with a shape operator. Use the following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $box|$polygon|$center : <coordinates>
                      } } } )

The following queries for documents within a rectangle defined by [ 0 , 0 ] at the bottom left corner and by [ 100 , 100 ] at the top right corner.

db.places.find( { loc :
                  { $geoWithin :
                     { $box : [ [ 0 , 0 ] ,
                                [ 100 , 100 ] ]
                 } } } )

The following queries for documents that are within the circle centered on [ -74 , 40.74 ] and with a radius of 10:

db.places.find( { loc: { $geoWithin :
                          { $center : [ [-74, 40.74 ] , 10 ]
                } } } )

For syntax and examples for each shape, see the following:

Points within a Circle Defined on a Sphere

MongoDB supports rudimentary spherical queries on flat 2d indexes for legacy reasons. In general, spherical calculations should use a 2dsphere index, as described in 2dsphere Indexes.

To query for legacy coordinate pairs in a “spherical cap” on a sphere, use $geoWithin with the $centerSphere operator. Specify an array that contains:

Use the following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $centerSphere : [ [ <x>, <y> ] , <radius> ] }
                      } } )

The following example query returns all documents within a 10-mile radius of longitude 88 W and latitude 30 N. The example converts distance to radians by dividing distance by the approximate radius of the earth, 3959 miles:

db.<collection>.find( { loc : { $geoWithin :
                                 { $centerSphere :
                                    [ [ 88 , 30 ] , 10 / 3959 ]
                      } } } )

Proximity to a Point on a Flat Surface

Proximity queries return the 100 legacy coordinate pairs closest to the defined point and sort the results by distance. Use either the $near operator or geoNear command. Both require a 2d index.

The $near operator uses the following syntax:

db.<collection>.find( { <location field> :
                         { $near : [ <x> , <y> ]
                      } } )

For examples, see $near.

The geoNear command uses the following syntax:

db.runCommand( { geoNear: <collection>, near: [ <x> , <y> ] } )

The geoNear command offers more options and returns more information than does the $near operator. To run the command, see geoNear.

Exact Matches on a Flat Surface

You can use the db.collection.find() method to query for an exact match on a location. These queries use the following syntax:

db.<collection>.find( { <location field>: [ <x> , <y> ] } )

This query will return any documents with the value of [ <x> , <y> ].