Hello,
I am trying to use FIRST in a select, in a doubly nested arrays context. Documents have typically the form:
{
“one”: [
{
“two”: [
{
“three”: 0
},
{
“three”: 1
}
]
},
{
“two”: [
{
“three”: 2
},
{
“three”: 3
}
]
}
]
}
one
and two
are both arrays, two
is nested in one
.
I would like to retrieve the first value of three
and the documents ids of the documents which respect some condition (say >1) on any of their three
values exactly in the one->two->three path (no other place in the document where other three
fields might appear). I want no more than one row per document (hence no UNNEST), and the simplest efficient select possible.
Creating the index does not seem to be an issue, I do as follows:
CREATE INDEX myindex
ON test
(DISTINCT ARRAY(DISTINCT ARRAY(v1.three
) FOR v1 IN v0.two
END) FOR v0 IN one
END)
Selecting, without use of UNNEST, and with the FIRST keyword is a challenge for me. I am probably missing something on the N1QL syntax. The WHERE clause making use of the index is quite easy:
WHERE ANY v0 IN one
SATISFIES ANY v1 IN v0.two
SATISFIES v1.three
>1 END END
It’s the beginning of the select which is unclear to me. I’d like something like (’…’ replaced by proper N1QL syntax):
SELECT FIRST v1.three
FOR v1 IN … WHEN v1.three
>1 END, meta().id FROM test
USE INDEX (myindex
) WHERE ANY v0 IN one
SATISFIES ANY v1 IN v0.two
SATISFIES v1.three
>1 END END
Trying something silly like this below actually returns the proper response, but is does not contain the value of the three
field.
SELECT FIRST v1.three
FOR v1 IN (ANY v0 IN one
SATISFIES 1=1 END) WHEN v1.three
>1 END, meta().id FROM test
USE INDEX (myindex
) WHERE ANY v0 IN one
SATISFIES ANY v1 IN v0.two
SATISFIES v1.three
>1 END END
Can somebody let me know how to achieve this?
Thanks a lot!