对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

geoWithin Operator

geoWithin

geoWithin操作符支持查询给定几何图形内的地理点。 即使索引定义中的indexShapes值为true ,也仅返回点。

您可以在以下位置查询点:

  • 圆形

  • 边界框

  • 多边形

指定要搜索的坐标时,必须先指定经度,然后指定纬度。 经度值可以介于-180180之间,两者均包括在内。 纬度值可以介于-9090之间,两者均包括在内。 坐标值可以是整数或双精度值。

注意

MongoDB Search 不支持以下内容:

  • 非默认坐标参考系 (CRS)

  • 平面 XY 坐标系(二维)

  • 坐标对 点表示法(即pointFieldName: [12, 34]

geoWithin 通过以下语法实现:

{
"$search": {
"index": <index name>, // optional, defaults to "default"
"geoWithin": {
"path": "<field-to-search>",
"box | circle | geometry": <object>,
"score": <score-options>
}
}
}

geoWithin 使用以下词条来构造查询:

字段
类型
说明
必要性

box

对象

指定搜索框左下角和右上角 GeoJSON 点的对象。该对象接受以下字段:

  • bottomLeft - GeoJSON 左下角点。

  • topRight - 右上角的 GeoJSON点。

要了解如何在 GeoJSON 对象中指定 GeoJSON 数据,请参阅GeoJSON 对象。

boxcirclegeometry是必需的。

视条件而定

circle

对象

对象,用于指定Atlas Search的中心点和半径(以米为单位)。 该对象包含以下GeoJSON字段:

  • center - 指定为 GeoJSON 的圆心。

  • radius - Radius(半径)是一个数字,以米为单位。 值必须大于或等于0

要了解如何在 GeoJSON 对象中指定 GeoJSON 数据,请参阅GeoJSON 对象。

circleboxgeometry是必需的。

视条件而定

geometry

GeoJSON 对象

GeoJSON对象,用于指定要在其中进行 的 MultiPolygon Polygon Atlas Search。必须将多边形指定为闭环,其中最后一个位置与第一个位置相同。

When calculating geospatial results, MongoDB Search geoShape and geoWithin operators and MongoDB $geoIntersects operator use different geometries. This difference can be seen in how MongoDB Search and MongoDB draw polygonal edges.

MongoDB Search 根据笛卡尔距离绘制多边形,笛卡尔距离是坐标参考系中两点之间的最短直线。

MongoDB draws polygons using the geodesic mode based on 2dsphere indexes that is built on top of a third-party library for geodesic types, or the flat mode, from 2d indexes. To learn more, see GeoJSON Objects.

对于涉及多边形的地理空间查询, MongoDB Search 和MongoDB可能会返回不同的结果。

要了解如何在 GeoJSON 对象中指定 GeoJSON 数据,请参阅GeoJSON 对象。

geometryboxcircle是必需的。

视条件而定

path

字符串或字符串数组

已索引的 geo 类型字段或要搜索的字段。

score

对象

分配给匹配搜索结果的分数。 默认情况下,结果中的分数1 。 您可以使用以下选项修改分数:

  • boost将生成的分数乘以给定数字。

  • constant将结果分数替换为给定数字。

  • function:使用给定的表达式替换结果分数。

有关在查询中使用 score 的信息,请参阅对结果中的文档进行评分

no

The following examples use the listingsAndReviews collection in the sample_airbnb database. If you have the sample dataset on your cluster, you can create a custom MongoDB Search index for geo type and run the example queries on your cluster.

address.locationlistingsAndReviews使用以下示例索引定义为collection中的字段编制索引:

1{
2 "mappings": {
3 "fields": {
4 "address": {
5 "fields": {
6 "location": {
7 "type": "geo"
8 }
9 },
10 "type": "document"
11 },
12 "property_type": {
13 "type": "token"
14 }
15 }
16 }
17}

以下查询使用geoWithin操作符和box字段在澳大利亚的边界框中搜索属性。

查询包括:

  • $limit stage to limit the output to 3 results.

  • $project stage to exclude all fields except name and address.

注意

您无需在MongoDB Search查询中指定名为 default 的索引。如果索引有任何其他名称,则必须指定 index字段。

以下查询使用geoWithin操作符和circle字段搜索加拿大指定坐标一英里半径范围内的房产。

查询包括:

  • $limit stage to limit the output to 3 results.

  • $project stage to exclude all fields except name and address, and for Compass examples, the required steps reference.

注意

您无需在MongoDB Search查询中指定名为 default 的索引。如果索引有任何其他名称,则必须指定 index字段。

以下示例使用 geoWithin 操作符和 geometry 字段搜索夏威夷州的房产。type 字段指定区域是 GeoJSON 多边形还是多边形集合。

注意

您无需在MongoDB Search查询中指定名为 default 的索引。如果索引有任何其他名称,则必须指定 index字段。