$minDistance - Amazon DocumentDB

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

$minDistance

$minDistance は、中心点から少なくとも指定された最小距離にあるドキュメントをフィルタリング$geoNearするために、 $nearSphereまたは と組み合わせて使用される検索演算子です。この演算子は Amazon DocumentDB でサポートされており、MongoDB の対応する関数と同様に機能します。

パラメータ

  • $minDistance: 結果にドキュメントを含めるための中心点からの最小距離 (メートル単位)。

例 (MongoDB シェル)

この例では、ワシントン州シアトルの特定の場所から半径 2 km 以内のすべてのレストランを見つけます。

サンプルドキュメントを作成する

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