DocumentDB - 如何 return 查询的 SELECT 部分中的距离?

DocumentDB - How do I return the distance in the SELECT part of the query?

我想return我的搜索坐标和我正在搜索的字段之间的距离。

例如,您可以使用此 "Query Playground": 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 开发人员说我可以从 WHERE 子句中取出整个 ST_DISTANCE 位,并将其与 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 混为一谈,但我没有取得任何进展,我也没有在 Google.

中找到我需要的东西

那我需要做什么?对我来说,在一定距离内查询是至关重要的,但如果我必须在客户端重新计算所有这些距离,它的用处就有限了。

查询必须使用 SELECT v, ST_DISTANCE(...) 代替 SELECT *, ST_DISTANCE(...)。与 ANSI-SQL 类似,DocumentDB 中的 SELECT 子句可以包含值列表或使用 *,但不能同时使用。

完整查询:

-- Find all volcanoes of type Stratovolcano
-- (http://www.sciencedaily.com/terms/stratovolcano.htm) 
-- that are within 100 km of Redmond, WA. 
SELECT v, ST_DISTANCE(v.Location, { 
    "type": "Point", 
    "coordinates": [-122.19, 47.36] 
    }) AS DistanceMetres
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"