$geoWithin - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$geoWithin

Amazon DocumentDB의 $geoWithin 연산자는 위치 데이터(GeoJSON 객체로 표시됨)가 다각형 또는 다중 다각형과 같이 지정된 셰이프 내에 완전히 포함된 문서를 찾는 데 사용됩니다. 이는 특정 지리적 리전 내에 있는 객체를 쿼리하는 데 유용합니다.

파라미터

  • $geometry: 쿼리할 셰이프를 나타내는 GeoJSON 객체입니다.

예제(MongoDB 쉘)

다음 예제에서는 $geoWithin 운영자를 사용하여 뉴욕 주 내에 있는 모든 공항을 찾는 방법을 보여줍니다.

샘플 문서 생성

// Insert state document db.states.insert({ "name": "New York", "loc": { "type": "Polygon", "coordinates": [[ [-79.76278, 45.0], [-73.94, 45.0], [-73.94, 40.5], [-79.76278, 40.5], [-79.76278, 45.0] ]] } }); // Insert airport documents db.airports.insert([ { "name": "John F. Kennedy International Airport", "type": "airport", "code": "JFK", "loc": { "type": "Point", "coordinates": [-73.7781, 40.6413] } }, { "name": "LaGuardia Airport", "type": "airport", "code": "LGA", "loc": { "type": "Point", "coordinates": [-73.8772, 40.7769] } }, { "name": "Buffalo Niagara International Airport", "type": "airport", "code": "BUF", "loc": { "type": "Point", "coordinates": [-78.7322, 42.9403] } } ]);

쿼리 예제

var state = db.states.findOne({"name": "New York"}); db.airports.find({ "loc": { "$geoWithin": { "$geometry": state.loc } } }, { "name": 1, "type": 1, "code": 1, "_id": 0 });

출력

[ { "name": "John F. Kennedy International Airport", "type": "airport", "code": "JFK" }, { "name": "LaGuardia Airport", "type": "airport", "code": "LGA" } ]

코드 예제

$geoWithin 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function findAirportsWithinState(stateName) { 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 stateDoc = await db.collection('states').findOne({ name: stateName }, { projection: { _id: 0, loc: 1 } }); const airportDocs = await db.collection('airports').find({ loc: { $geoWithin: { $geometry: stateDoc.loc } } }, { projection: { name: 1, type: 1, code: 1, _id: 0 } }).toArray(); console.log(airportDocs); await client.close(); } findAirportsWithinState('New York');
Python
from pymongo import MongoClient def find_airports_within_state(state_name): try: client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] state_doc = db.states.find_one({'name': state_name}, {'_id': 0, 'loc': 1}) airport_docs = db.airports.find({ 'loc': { '$geoWithin': { '$geometry': state_doc['loc'] } } }, {'name': 1, 'type': 1, 'code': 1, '_id': 0}) return list(airport_docs) except Exception as e: print(f'Error: {e}') finally: client.close() airports = find_airports_within_state('New York') print(airports)