2d
Index2d
索引On this page本页内容
The following sections describe queries supported by the 以下部分描述2d
index.2d
索引支持的查询。
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:有关每个形状的语法和示例,请参阅以下内容:
MongoDB supports rudimentary spherical queries on flat 出于传统原因,MongoDB支持对平面2d
indexes for legacy reasons. 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 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> ] } } )
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> ]
的任何文档。