DocumentDB 获取子数组的最小值
DocumentDB get min of subarray
我的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)
这是我当前将经纬度转换为点的查询,但不知道如何获得 minimum/lowest 价格。
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-functions
发现 url 在
对于用户定义的函数 (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
据我所知,您目前只能使用 UDF 来实现您的要求。另外,我检查了 Larry Maccherone 提供的代码,它可以在 Azure DocumentDB 服务和我的 DocumentDB 模拟器(版本 1.11.136.2)上工作,如下所示:
DocumentDB.GatewayService.exe has stopped working
对于 DocumentDB.GatewayService 崩溃,我假设您需要收集转储文件并将它们通过电子邮件附加到 askdocdb@microsoft.com。详情请参考DocumentDB Emulator troubleshooting.
我的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)
这是我当前将经纬度转换为点的查询,但不知道如何获得 minimum/lowest 价格。
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-functions
发现 url 在
对于用户定义的函数 (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
据我所知,您目前只能使用 UDF 来实现您的要求。另外,我检查了 Larry Maccherone 提供的代码,它可以在 Azure DocumentDB 服务和我的 DocumentDB 模拟器(版本 1.11.136.2)上工作,如下所示:
DocumentDB.GatewayService.exe has stopped working
对于 DocumentDB.GatewayService 崩溃,我假设您需要收集转储文件并将它们通过电子邮件附加到 askdocdb@microsoft.com。详情请参考DocumentDB Emulator troubleshooting.