Query a 2d Index查询2d索引

On this page本页内容

The following sections describe queries supported by the 2d index.以下部分描述2d索引支持的查询。

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. 要选择平面上给定形状内找到的所有旧坐标对,请使用$geoWithin运算符和形状运算符。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.以下查询由左下角的[0, 0]和右上角的[100, 100]定义的矩形内的文档。

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:以下查询位于以[ -74 , 40.74 ]为中心且半径为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. 出于传统原因,MongoDB支持对平面2d索引的基本球形查询。In general, spherical calculations should use a 2dsphere index, as described in 2dsphere Indexes.通常,球面计算应使用2dsphere索引,如2dsphere索引中所述。

To query for legacy coordinate pairs in a "spherical cap" on a sphere, use $geoWithin with the $centerSphere operator. 要查询球体上“球形帽”中的旧坐标对,请将$geoWithin$centerSphere运算符一起使用。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. 以下示例查询返回经度88 W、纬度30 N的10英里半径内的所有文档。The example converts distance to radians by dividing distance by the approximate equatorial radius of the earth, 3963.2 miles:该示例通过将距离除以地球的近似赤道半径3963.2英里,将距离转换为弧度:

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

Proximity to a Point on a Flat Surface接近平面上的点

Proximity queries return the legacy coordinate pairs closest to the defined point and sort the results by distance. 接近度查询返回最接近定义点的旧坐标对,并按距离对结果进行排序。Use either the $near operator. 使用$near运算符。The operator requires a 2d index.运算符需要2d索引。

The $near operator uses the following syntax:$near运算符使用以下语法:

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

For examples, see $near.有关示例,请参阅$near

Exact Matches on a Flat Surface平面上的精确匹配

You cannot use a 2d index to return an exact match for a coordinate pair. 不能使用2d索引返回坐标对的精确匹配。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:在下面的示例中,如果您有{'loc': 1}索引,find()操作将返回一个位置的精确匹配:

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

This query will return any documents with the value of [ <x> , <y> ].此查询将返回值为[ <x> , <y> ]的任何文档。

←  Create a 2d Index2d Index Internals →