Docs HomeMongoDB Manual

# Query a .leafygreen-ui-1nwfx0p{font-size:15px;line-height:24px;-webkit-transition:all 0.15s ease-in-out;transition:all 0.15s ease-in-out;border-radius:3px;font-family:'Source Code Pro',Menlo,monospace;line-height:20px;display:inherit;background-color:#F9FBFA;border:1px solid #E8EDEB;color:#1C2D38;white-space:nowrap;font-size:unset;display:inline;}.lg-ui-0000:hover>.leafygreen-ui-1nwfx0p{-webkit-text-decoration:none;text-decoration:none;}.lg-ui-0000:hover>.leafygreen-ui-1nwfx0p{box-shadow:0 0 0 3px #E8EDEB;border:1px solid #C1C7C6;}a .leafygreen-ui-1nwfx0p{color:inherit;}`2d` Index.css-134mg1q{-webkit-align-self:center;-ms-flex-item-align:center;align-self:center;padding:0 10px;visibility:hidden;}.css-6vrlzm{border-radius:0!important;display:initial!important;margin:initial!important;}.css-1l4s55v{margin-top:-175px;position:absolute;padding-bottom:2px;}

• Points within a Shape Defined on a Flat Surface
• Points within a Circle Defined on a Sphere
• Proximity to a Point on a Flat Surface
• Exact Matches on a Flat Surface

The following sections describe queries supported by the `2d` index.

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:

`.leafygreen-ui-1xcl9uc{color:inherit;font-size:13px;font-family:'Source Code Pro',Menlo,monospace;line-height:24px;font-size:15px;}`.leafygreen-ui-1v41da1{border-spacing:0;width:100%;}.leafygreen-ui-7razhx{border-spacing:0;vertical-align:top;padding:0 16px;}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:

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 equatorial radius of the earth, 3963.2 miles:

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

Proximity queries return the legacy coordinate pairs closest to the defined point and sort the results by distance. Use either the `\$near` operator. The operator requires a `2d` index.

The `\$near` operator uses the following syntax:

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

For examples, see `\$near`.

You cannot use a `2d` index to return an exact match for a coordinate pair. Use a scalar, ascending or descending, index on a field that stores coordinates to return exact matches.

In the following example, the `find()` operation will return an exact match on a location if you have a ```{ 'loc': 1}``` index:

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

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

←  Create a `2d` Index`2d` Index Internals →
Share Feedback