SELECT ARRYA_FLATTEN(ARRAY (ARRAY u1.rhsId FOR u1 IN d2.entityRelations WHEN u1. lhsId = v1.id END) FOR v1 IN d. entities WHEN v1.name = "foo" END,2)
FROM default AS d USE KEYS "doc1"
LET d2 = (SELECT d2.* FROM default AS d2 USE KEYS "doc2")[0]
Thanks for the quick response.
The problem is a little more complex, which I should have been more clear on.
The join I want to perform on is on two different result sets obtained from two different queries. So the documents (1)entities and (2) entityRelations are a result of some other N1QL query(See rephrased question below)
I have 3 entity documents and 2 relationship documents
Entities:
SELECT
ARRYA_FLATTEN(ARRAY
(ARRAY u1.rhsId FOR u1 IN entityRelations WHEN u1. lhsId = v1.id END)
FOR v1 IN entities WHEN v1.name = "foo" END,2)
LET entityRelations = (Sub query),
entities = (SUB query);
One question though. Does this mean two separate queries are performed by couchbase and the result sets are later merged.
Or does couchbase optimise it by using the results of the entities sub query in the entityRelations subquery.
It is single query and has subqueries. subquries are optimized separately. Optimization done at prepare time and results are available at execution time, so results are not used for optimization.
If you need that client needs to separate the queries provide the result has query named/positional parameters.
If you have already have you can project
ARRYA_FLATTEN(ARRAY
(ARRAY {u1.rhsId, v1.id, v1.name,…} FOR u1 IN entityRelations WHEN u1. lhsId = v1.id END)
FOR v1 IN entities WHEN v1.name = “foo” END,2)