Cosmos DB : Find the index of an item in an array

Viewed 296

I want to find the index number of all items in a nested array in Cosmos DB :

Data :

{
"id":"MyId",
"items" : [
{"id": "Item1"},
{"id" : "Item2"}
]
}

Query :

SELECT f.id, index : ** How to get the index of items ? ** 

FROM root JOIN f IN root["items"] 

WHERE (IS_DEFINED(root["items"]) AND (root["id"] = "MyId"))

I would like a result like this :

[

{"id": "item1", "index": 0},

{"id": "item2", "index" : 1}

]

Thx

1 Answers

There is no built in support on Cosmos SQL API to achieve the above result. But you can implement the following suggestions

  1. You could either write your own logic in User Defined Function or retrieve the data and format it in the way you need on the Client Side

  2. Other way is to just include the index in the data model itself

Related