$geometry - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$geometry

$geometryOperator di Amazon DocumentDB digunakan untuk menentukan objek geometri GeoJSON sebagai bagian dari kueri geospasial. Operator ini digunakan bersama dengan operator kueri geospasial lainnya seperti $geoWithin dan $geoIntersects untuk melakukan kueri spasial pada data Anda.

Di Amazon DocumentDB, operator mendukung jenis $geometry geometri GeoJSON berikut:

  • Poin

  • LineString

  • Polygon

  • MultiPoint

  • MultiLineString

  • MultiPolygon

  • GeometryCollection

Parameter

  • type: Jenis objek geometri GeoJSON, misalnya,,, dll. Point Polygon

  • coordinates: Sebuah array koordinat yang mewakili geometri. Struktur array koordinat tergantung pada jenis geometri.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan cara menggunakan $geometry operator untuk melakukan $geoIntersects query di Amazon DocumentDB.

Buat dokumen sampel

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] ]] } } ]);

Contoh kueri

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] ]] } } } })

Keluaran

[ { "_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] ] ] } } ]

Contoh kode

Untuk melihat contoh kode untuk menggunakan $geometry perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

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