Docs 菜单
Docs 主页
/
MongoDB Atlas
/ / / /

geoWithin

在此页面上

  • 定义
  • 语法
  • 选项
  • 举例
  • box 例子
  • circle 例子
  • geometry 举例
geoWithin

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

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

  • 圆形

  • 边界框

  • 多边形

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

注意

Atlas 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。必须将多边形指定为闭环,其中最后一个位置与第一个位置相同。

计算地理空间结果时,Atlas Search geoShapegeoWithin操作符以及 MongoDB $geoIntersects操作符使用不同的几何图形。从 Atlas Search 和 MongoDB 绘制多边形边的方式可以看出这种差异。

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

MongoDB 使用适用于 测地线类型 的第三方库绘制多边形 使用测地线。要了解详情,请参阅GeoJSON 对象。

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

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

geometryboxcircle是必需的。

视条件而定
path
字符串或字符串数组
要搜索的一个或多个带索引的地理类型字段。 请参阅路径构造。
score
对象

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

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

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

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

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

no

listingsAndReviewssample_airbnb以下示例使用数据库中的collection集合。如果集群上有样本数据集,则可以为地理类型创建自定义 Atlas Search 索引,并在集群上运行示例查询。

提示

如果您已经加载示例数据集,请按照 Atlas Search 入门教程,创建索引定义并运行 Atlas Search 查询。

您可以使用以下任何样本数据集通过geoWithin操作符运行 Atlas Search 搜索查询:

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

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

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

查询包括:

  • $limit 阶段将输出限制为 3 个结果。

  • $project 阶段排除 nameaddress 以外的所有字段。

注意

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

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoWithin": {
5 "path": "address.location",
6 "box": {
7 "bottomLeft": {
8 "type": "Point",
9 "coordinates": [112.467, -55.050]
10 },
11 "topRight": {
12 "type": "Point",
13 "coordinates": [168.000, -9.133]
14 }
15 }
16 }
17 }
18 },
19 {
20 $limit: 3
21 },
22 {
23 $project: {
24 "_id": 0,
25 "name": 1,
26 "address": 1
27 }
28 }
29])

查询返回以下结果:

1{
2 "name" : "Surry Hills Studio - Your Perfect Base in Sydney",
3 "address" : {
4 "street" : "Surry Hills, NSW, Australia",
5 "suburb" : "Darlinghurst",
6 "government_area" : "Sydney",
7 "market" : "Sydney",
8 "country" : "Australia",
9 "country_code" : "AU",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ 151.21554, -33.88029 ],
13 "is_location_exact" : true
14 }
15 }
16}
17{
18 "name" : "Sydney Hyde Park City Apartment (checkin from 6am)",
19 "address" : {
20 "street" : "Darlinghurst, NSW, Australia",
21 "suburb" : "Darlinghurst",
22 "government_area" : "Sydney",
23 "market" : "Sydney",
24 "country" : "Australia",
25 "country_code" : "AU",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ 151.21346, -33.87603 ],
29 "is_location_exact" : false
30 }
31 }
32}
33{
34 "name" : "THE Place to See Sydney's FIREWORKS",
35 "address" : {
36 "street" : "Rozelle, NSW, Australia",
37 "suburb" : "Lilyfield/Rozelle",
38 "government_area" : "Leichhardt",
39 "market" : "Sydney",
40 "country" : "Australia",
41 "country_code" : "AU",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ 151.17956, -33.86296 ],
45 "is_location_exact" : true
46 }
47 }
48}

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

查询包括:

  • $limit阶段,用于将输出限制为3结果

  • $project 阶段排除 nameaddress 以外的所有字段。

注意

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

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoWithin": {
5 "circle": {
6 "center": {
7 "type": "Point",
8 "coordinates": [-73.54, 45.54]
9 },
10 "radius": 1600
11 },
12 "path": "address.location"
13 }
14 }
15 },
16 {
17 $limit: 3
18 },
19 {
20 $project: {
21 "_id": 0,
22 "name": 1,
23 "address": 1
24 }
25 }
26])

查询返回以下结果:

1{
2 "name" : "Ligne verte - à 15 min de métro du centre ville.",
3 "address" : {
4 "street" : "Montréal, Québec, Canada",
5 "suburb" : "Hochelaga-Maisonneuve",
6 "government_area" : "Mercier-Hochelaga-Maisonneuve",
7 "market" : "Montreal",
8 "country" : "Canada",
9 "country_code" : "CA",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -73.54949, 45.54548 ],
13 "is_location_exact" : false
14 }
15 }
16}
17{
18 "name" : "Belle chambre à côté Metro Papineau",
19 "address" : {
20 "street" : "Montréal, QC, Canada",
21 "suburb" : "Gay Village",
22 "government_area" : "Ville-Marie",
23 "market" : "Montreal",
24 "country" : "Canada",
25 "country_code" : "CA",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ -73.54985, 45.52797 ],
29 "is_location_exact" : false
30 }
31 }
32}
33{
34 "name" : "L'IDÉAL, ( à 2 min du métro Pie-IX ).",
35 "address" : {
36 "street" : "Montréal, Québec, Canada",
37 "suburb" : "Mercier-Hochelaga-Maisonneuve",
38 "government_area" : "Mercier-Hochelaga-Maisonneuve",
39 "market" : "Montreal",
40 "country" : "Canada",
41 "country_code" : "CA",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ -73.55208, 45.55157 ],
45 "is_location_exact" : true
46 }
47 }
48}

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

查询包括:

  • $limit 阶段将输出限制为 3 个结果。

  • $project 阶段排除 nameaddress 以外的所有字段。

注意

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

1 db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoWithin": {
5 "geometry": {
6 "type": "Polygon",
7 "coordinates": [[[ -161.323242, 22.512557 ],
8 [ -152.446289, 22.065278 ],
9 [ -156.09375, 17.811456 ],
10 [ -161.323242, 22.512557 ]]]
11 },
12 "path": "address.location"
13 }
14 }
15 },
16 {
17 $limit: 3
18 },
19 {
20 $project: {
21 "_id": 0,
22 "name": 1,
23 "address": 1
24 }
25 }
26 ])

查询返回以下结果:

1{
2 "name" : "Ocean View Waikiki Marina w/prkg",
3 "address" : {
4 "street" : "Honolulu, HI, United States",
5 "suburb" : "Oʻahu",
6 "government_area" : "Primary Urban Center",
7 "market" : "Oahu",
8 "country" : "United States",
9 "country_code" : "US",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -157.83919, 21.28634 ],
13 "is_location_exact" : true
14 }
15 }
16}
17{
18 "name" : "Kailua-Kona, Kona Coast II 2b condo",
19 "address" : {
20 "street" : "Kailua-Kona, HI, United States",
21 "suburb" : "Kailua/Kona",
22 "government_area" : "North Kona",
23 "market" : "The Big Island",
24 "country" : "United States",
25 "country_code" : "US",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ -155.96445, 19.5702 ],
29 "is_location_exact" : true
30 }
31 }
32}
33{
34 "name" : "LAHAINA, MAUI! RESORT/CONDO BEACHFRONT!! SLEEPS 4!",
35 "address" : {
36 "street" : "Lahaina, HI, United States",
37 "suburb" : "Maui",
38 "government_area" : "Lahaina",
39 "market" : "Maui",
40 "country" : "United States",
41 "country_code" : "US",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ -156.68012, 20.96996 ],
45 "is_location_exact" : true
46 }
47 }
48}
1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoWithin": {
5 "geometry": {
6 "type": "MultiPolygon",
7 "coordinates": [
8 [[[-157.8412413882,21.2882235819],
9 [-157.8607925468,21.2962046205],
10 [-157.8646640634,21.3077019651],
11 [-157.862776699,21.320776283],
12 [-157.8341758705,21.3133826738],
13 [-157.8349985678,21.3000822569],
14 [-157.8412413882,21.2882235819]]],
15 [[[-157.852898124,21.301208833],
16 [-157.8580050499,21.3050871833],
17 [-157.8587346108,21.3098050385],
18 [-157.8508811028,21.3119240258],
19 [-157.8454308541,21.30396767],
20 [-157.852898124,21.301208833]]]
21 ]
22 },
23 "path": "address.location"
24 }
25 }
26 },
27 {
28 $limit: 3
29 },
30 {
31 $project: {
32 "_id": 0,
33 "name": 1,
34 "address": 1
35 }
36 }
37])

查询返回以下结果:

1{
2 "name" : "Heart of Honolulu, 2BD gem! Free Garage Parking!",
3 "address" : {
4 "street" : "Honolulu, HI, United States",
5 "suburb" : "Makiki/Lower Punchbowl/Tantalus",
6 "government_area" : "Primary Urban Center",
7 "market" : "Oahu",
8 "country" : "United States",
9 "country_code" : "US",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -157.84343, 21.30852 ],
13 "is_location_exact" : false
14 }
15 }
16}
17{
18 "name" : "Private Studio closed to town w/ compact parking",
19 "address" : {
20 "street" : "Honolulu, HI, United States",
21 "suburb" : "Oʻahu",
22 "government_area" : "Primary Urban Center",
23 "market" : "Oahu",
24 "country" : "United States",
25 "country_code" : "US",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ -157.85228, 21.31184 ],
29 "is_location_exact" : true
30 }
31 }
32}
33{
34 "name" : "Comfortable Room (2) at Affordable Rates",
35 "address" : {
36 "street" : "Honolulu, HI, United States",
37 "suburb" : "Oʻahu",
38 "government_area" : "Primary Urban Center",
39 "market" : "Oahu",
40 "country" : "United States",
41 "country_code" : "US",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ -157.83889, 21.29776 ],
45 "is_location_exact" : false
46 }
47 }
48}

后退

geoShape