我想返回搜索座標和我正在搜索的字段之間的距離。DocumentDB - 如何在查詢的SELECT部分返回距離?
舉個例子,你可以使用這個 「查詢遊樂場」: https://www.documentdb.com/sql/demo#geospatial
他們使用下面的示例查詢:
-- Find all volcanoes of type Stratovolcano
-- (http://www.sciencedaily.com/terms/stratovolcano.htm)
-- that are within 100 km of Redmond, WA.
SELECT *
FROM volcanoes v
WHERE ST_DISTANCE(v.Location, {
"type": "Point",
"coordinates": [-122.19, 47.36]
}) < 100 * 1000
AND v.Type = "Stratovolcano"
AND v["Last Known Eruption"] = "Last known eruption from 1800-1899, inclusive"
,他們得到的結果如下:
{
"Volcano Name": "Rainier",
"Country": "United States",
"Region": "US-Washington",
"Location": {
"type": "Point",
"coordinates": [
-121.758,
46.87
]
},
"Elevation": 4392,
"Type": "Stratovolcano",
"Status": "Dendrochronology",
"Last Known Eruption": "Last known eruption from 1800-1899, inclusive",
"id": "33eff74b-e331-bca5-bf32-f8ece733465a",
"_rid": "FX8tANMM6QEeBAAAAAAAAA==",
"_ts": 1438994836,
"_self": "dbs/FX8tAA==/colls/FX8tANMM6QE=/docs/FX8tANMM6QEeBAAAAAAAAA==/",
"_etag": "\"00008304-0000-0000-0000-55c551940000\"",
"_attachments": "attachments/"
}
假設我想將火山(在[-121.758, 46.87]
)和搜索座標之間的距離以米爲單位[-122.19, 47.36]
在我的T-SQL開發人員說,我可以把整個ST_DISTANCE
位來自WHERE
條款,幷包括其與SELECT
這樣一起:
-- Find all volcanoes of type Stratovolcano
-- (http://www.sciencedaily.com/terms/stratovolcano.htm)
-- that are within 100 km of Redmond, WA.
SELECT *, ST_DISTANCE(v.Location, {
"type": "Point",
"coordinates": [-122.19, 47.36]
})
FROM volcanoes v
WHERE ST_DISTANCE(v.Location, {
"type": "Point",
"coordinates": [-122.19, 47.36]
}) < 100 * 1000
AND v.Type = "Stratovolcano"
AND v["Last Known Eruption"] = "Last known eruption from 1800-1899, inclusive"
但是不起作用,它只是給我一個語法錯誤:
{
"errors": [
{
"severity": "Error",
"location": {
"start": 153,
"end": 154
},
"code": "SC1001",
"message": "Syntax error, incorrect syntax near ','."
}
]
}
我已經嘗試過許多東西,比如v.*
,走樣的ST_DISTANCE
結果與AS
,但我不在任何地方,我都沒有找到我需要的谷歌。
那麼我需要做什麼?對於我來說,在一定距離內查詢是至關重要的,但如果我不得不重新計算客戶端的所有距離,那麼它的使用是有限的。
我已經編輯你的答案來解釋爲什麼*是不同的。你能否把它標記爲答案? –