$geometry - Amazon DocumentDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$geometry

Amazon DocumentDB 中的$geometry运算符用于在地理空间查询中指定 GeoJSON 几何对象。此运算符可与其他地理空间查询运算符(如$geoWithin和)结合使用$geoIntersects,对您的数据执行空间查询。

在亚马逊 DocumentDB 中,$geometry运算符支持以下 GeoJSON 几何类型:

  • Point

  • LineString

  • Polygon

  • MultiPoint

  • MultiLineString

  • MultiPolygon

  • GeometryCollection

参数

  • type: geoJSON 几何对象的类型,例如PointPolygon、等

  • coordinates:表示几何形状的坐标数组。坐标数组的结构取决于几何类型。

示例(MongoDB 外壳)

以下示例演示如何使用$geometry运算符在 Amazon DocumentDB 中执行$geoIntersects查询。

创建示例文档

db.locations.insertMany([ { "_id": 1, "name": "Location 1", "location": { "type": "Point", "coordinates": [-73.983253, 40.753941] } }, { "_id": 2, "name": "Location 2", "location": { "type": "Polygon", "coordinates": [[ [-73.998427, 40.730309], [-73.954348, 40.730309], [-73.954348, 40.780816], [-73.998427, 40.780816], [-73.998427, 40.730309] ]] } } ]);

查询示例

db.locations.find({ "location": { "$geoIntersects": { "$geometry": { "type": "Polygon", "coordinates": [[ [-73.998, 40.730], [-73.954, 40.730], [-73.954, 40.781], [-73.998, 40.781], [-73.998, 40.730] ]] } } } })

输出

[ { "_id": 2, "name": "Location 2", "location": { "type": "Polygon", "coordinates": [ [ [-73.998427, 40.730309], [-73.954348, 40.730309], [-73.954348, 40.780816], [-73.998427, 40.780816], [-73.998427, 40.730309] ] ] } } ]

代码示例

要查看使用该$geometry命令的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('locations'); const query = { "location": { "$geoIntersects": { "$geometry": { "type": "Polygon", "coordinates": [[ [-73.998, 40.730], [-73.954, 40.730], [-73.954, 40.781], [-73.998, 40.781], [-73.998, 40.730] ]] } } } }; const result = await collection.find(query).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['locations'] query = { "location": { "$geoIntersects": { "$geometry": { "type": "Polygon", "coordinates": [[ [-73.998, 40.730], [-73.954, 40.730], [-73.954, 40.781], [-73.998, 40.781], [-73.998, 40.730] ]] } } } } result = list(collection.find(query)) print(result) client.close() example()