我有这样的文档:
{ "id": ....,
"Title": ""title,
"ZipCodes": [
{
"Code": "code01",
"Name": "Name01"
},
{
"Code": "code02",
"Name": "Name02"
},
{
"Code": "code03",
"Name": "Name03"
} ],
"_rid": .......,
"_self": .......,
"_etag": ......,
"_attachments": "attachments/",
"_ts": ......
我被用来指挥
select c.id, c.ZipCodes[ARRAY_LENGTH (c.ZipCodes) -1] as ZipCodes from c
但是我收到错误,如何查询 cosmos DB 中的最后一个元素 ZipCodes。
最佳答案
您可以使用ARRAY_SLICE
为了这。当传递 -1
时,它返回一个包含原始数组最后一个元素的数组。然后使用 [0]
对其进行索引以获取包含的单个元素(即邮政编码本身。)
SELECT c.id,
ARRAY_SLICE(c.ZipCodes,-1)[0] AS LastZipCode
FROM c
关于azure - 查询cosmosDB : get last element in array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50632877/