本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$minDistance
$minDistance 是與 $nearSphere或 搭配使用的尋找運算子$geoNear,用於篩選至少與中心點指定最小距離的文件。Amazon DocumentDB 支援此運算子,其函數類似於 MongoDB 中的對應函數。
參數
-
$minDistance:在結果中包含文件的中心點最小距離 (公尺)。
範例 (MongoDB Shell)
在此範例中,我們會在華盛頓州西雅圖特定位置的 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命令的程式碼範例,請選擇您要使用的語言標籤: