本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$geoWithin
Amazon DocumentDB 中的$geoWithin運算子用於尋找位置資料 (表示為 GeoJSON 物件) 完全包含在指定形狀內的文件,例如多邊形或多邊形。這對於查詢位於特定地理區域內的物件非常有用。
參數
-
$geometry:GeoJSON 物件,代表要查詢的形狀。
範例 (MongoDB Shell)
下列範例示範如何使用 $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命令的程式碼範例,請選擇您要使用的語言標籤: