having flight code I need to get it’s index at json array.
For example: for AF678 → 0, for AF392 → 1, for AF114 → 2.
I’m looking for some N1QL query to retrive such info.
It is required to make sub-doc mutation request on the next step:
Thx. Might you also know how to get the follows.
Let’s suppose that flight code ‘AF114’ from first json was replaced to ‘AF678’.
So now it contains two elements are matched condition flight = “AF678”.
I need to consuct n1ql request to get next structure:
[
{
“id”: “route_10002”,
“position”: 0,
“utc”: “15:05:00”
},
{
“id”: “route_10002”,
“position”: 2,
“utc”: “07:35:00”
}
]
I tried with n1ql:
SELECT meta().id, sh.utc,
FIRST ARRAY_POS(t.schedule, v) FOR v IN t.schedule WHEN v.flight = "AF678" END AS position
FROM `travel-sample` t UNNEST t.schedule sh
WHERE meta().id = 'route_10002' AND t.type='route' AND sh.flight = "AF678"
It produces something similar but only first array position:
I tried one more:
SELECT meta().id,
ARRAY v.utc FOR v IN t.schedule WHEN v.flight = "AF678" END AS utc,
ARRAY ARRAY_POS(t.schedule, v) FOR v IN t.schedule WHEN v.flight = "AF678" END AS position
FROM `travel-sample` t
WHERE meta().id = 'route_10002' AND t.type='route'
SELECT meta(t).id, sh.utc, sh.position
FROM `travel-sample` AS t
UNNEST ARRAY OBJECT_ADD(v, "position",pos ) FOR pos IN ARRAY_RANGE(0, ARRAY_LENGTH(t.schedule)) , v IN t.schedule END AS sh
WHERE meta(t).id = 'route_10002' AND t.type='route' AND sh.flight = "AF678";
Out of interest; what issue do you have using Sub-Doc for this? It’ll most likely be faster than N1QL, and doesn’t require an array index to be created.
I needed to make multi update at docs where data is contained inside an array (route doc with schedule at travel-sample bucket as an example). I tried using-array-index-to-speed-up-update-query with array index and indeed it’s more slower than sub-doc with mutation. You are right.