DocumentDB得到子阵列的最小值

时间:2017-04-20 13:30:16

标签: json azure-cosmosdb nosql

我的json数据:

[
    {
    "Code": "GB-00001",
    "BasicInformation": {
        "WGS84Longitude": -4.670000,
        "WGS84Latitude": 50.340000
     },
     "Availability": [{
            "ArrivalDate": "2017-04-21",
            "Price": 689
        },
        {
            "ArrivalDate": "2017-04-28",
            "Price": 1341
        }
     ]},
    {
    "Code": "GB-00002",
    "BasicInformation": {
        "WGS84Longitude": -4.680000,
        "WGS84Latitude": 50.350000
     },
     "Availability": [{
            "ArrivalDate": "2017-04-21",
            "Price": 659
        },
        {
            "ArrivalDate": "2017-04-28",
            "Price": 1440
        }
     ]}
}]

我希望结果如下:

[
{
    "HouseCode": "GB-00001",
    "Country": "GB",
    "location": {
        "type": "Point",
        "coordinates": [
            50.340000,
            -4.670000
        ]
    }, "lowestPrice": 689
},
{
    "HouseCode": "GB-00002",
    "Country": "GB",
    "location": {
        "type": "Point",
        "coordinates": [
            50.350000,
            -4.680000
        ]
    }, "lowestPrice" : 659
}

我的问题是:如何使用min(c.Availability.Price)

这是我当前的查询,将lat lng转换为point,但不知道如何获得最低/最低价格。

SELECT c.Code, c.BasicInformation.Country , 
    {"type":"Point","coordinates": [c.BasicInformation.Latitude, c.BasicInformation.Longitude]} as location
FROM c 

已尝试使用Join c.Availability a, min(a.Price)

编辑或许我太早了? https://feedback.azure.com/forums/263030-documentdb/suggestions/18561901-add-group-by-support-for-aggregate-functionshttps://stackoverflow.com/a/42697673/169714

中找到了该网址

2 个答案:

答案 0 :(得分:2)

这非常接近用户定义函数(UDF)的理想情况。

这是应该做的诀窍:

function lowestPrice(availability) {
  var i, len, lowest, row;
  lowest = 2e308;
  for (i = 0, len = availability.length; i < len; i++) {
    row = availability[i];
    lowest = Math.min(lowest, row.Price);
  }
  return lowest;
};

你这样称呼它:

SELECT 
  c.Code, 
  c.BasicInformation.Country, 
  {"type":"Point","coordinates": [
    c.BasicInformation.Latitude, c.BasicInformation.Longitude
  ]} as location,
  udf.lowestPrice(c.Availability) as lowestPrice
FROM c 

答案 1 :(得分:1)

AFAIK,您现在只能使用UDF来满足您的要求。另外,我检查了Larry Maccherone提供的代码,它可以在Azure DocumentDB服务和我的DocumentDB Emulator(版本1.11.136.2)上运行,如下所示:

enter image description here

  

DocumentDB.GatewayService.exe已停止工作

对于DocumentDB.GatewayService崩溃,我假设您需要收集转储文件并通过电子邮件将其附加到askdocdb@microsoft.com。有关详细信息,请参阅DocumentDB Emulator troubleshooting