Position of key in use keys clause and update

Hi!

Can i do something like this in n1ql?

update bucket use keys [“id::a”, “id::b” ] set num = (position of id)** in array_position([“id::a”, “id::b”], id) returning bucket.num;

** i need this to be the position of the key in the array

If for example before statement execution the documents was:
id::a {
“num”: 0
}

id::b{
“num”: 0
}

After the execution will be:
id::a {
“num”: 1
}

id::b{
“num”: 2
}

Something like that should work. Have you tried it out?

I don’t know how to get the position of an element in array and then pass it as new value to updated field. Any ideas?

ARRAY_POSITION() returns the position within the array. So you can do:

UPDATE …
SET my_pos_field = ARRAY_POSITION(my_array, my_id)
USE KEYS / WHERE / etc.

Thank you @geraldss!

1 Like