$minDistance - Amazon DocumentDB

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

$minDistance

$minDistance는 중심점에서 지정된 최소 거리 이상인 문서를 필터링$geoNear하기 위해 $nearSphere 또는와 함께 사용되는 찾기 연산자입니다. 이 연산자는 Amazon DocumentDB에서 지원되며 MongoDB의 해당 연산자와 유사하게 작동합니다.

파라미터

  • $minDistance: 결과에 문서를 포함할 중심점으로부터의 최소 거리(미터)입니다.

예제(MongoDB 쉘)

이 예제에서는 워싱턴주 시애틀의 특정 위치에서 반경 2킬로미터 이내에 있는 모든 레스토랑을 찾습니다.

샘플 문서 생성

db.usarestaurants.insertMany([ { "state": "Washington", "city": "Seattle", "name": "Noodle House", "rating": 4.8, "location": { "type": "Point", "coordinates": [-122.3517, 47.6159] } }, { "state": "Washington", "city": "Seattle", "name": "Pike Place Grill", "rating": 4.5, "location": { "type": "Point", "coordinates": [-122.3412, 47.6102] } }, { "state": "Washington", "city": "Bellevue", "name": "The Burger Joint", "rating": 4.2, "location": { "type": "Point", "coordinates": [-122.2007, 47.6105] } } ]);

쿼리 예제

db.usarestaurants.find({ "location": { "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [-122.3516, 47.6156] }, "$minDistance": 1, "$maxDistance": 2000 } } }, { "name": 1 });

출력

{ "_id" : ObjectId("611f3da985009a81ad38e74b"), "name" : "Noodle House" } { "_id" : ObjectId("611f3da985009a81ad38e74c"), "name" : "Pike Place Grill" }

코드 예제

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

Node.js
const { MongoClient } = require('mongodb'); async function findRestaurantsNearby() { 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('usarestaurants'); const result = await collection.find({ "location": { "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [-122.3516, 47.6156] }, "$minDistance": 1, "$maxDistance": 2000 } } }, { "projection": { "name": 1 } }).toArray(); console.log(result); client.close(); } findRestaurantsNearby();
Python
from pymongo import MongoClient def find_restaurants_nearby(): 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.usarestaurants result = list(collection.find({ "location": { "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [-122.3516, 47.6156] }, "$minDistance": 1, "$maxDistance": 2000 } } }, { "projection": {"name": 1} })) print(result) client.close() find_restaurants_nearby()