$geometry - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$geometry

Amazon DocumentDB の $geometry演算子は、地理空間クエリの一部として GeoJSON ジオメトリオブジェクトを指定するために使用されます。この演算子は、 $geoWithinや などの他の地理空間クエリ演算子と組み合わせて$geoIntersects使用され、データに対して空間クエリを実行します。

Amazon DocumentDB では、$geometry演算子は次の GeoJSON ジオメトリタイプをサポートしています。

  • Point

  • LineString

  • Polygon

  • MultiPoint

  • MultiLineString

  • MultiPolygon

  • GeometryCollection

パラメータ

  • type: GeoJSON ジオメトリオブジェクトのタイプ。例: 、 Point Polygonなど。

  • 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()