Hi, I’m trying to perform this query with N1QL, but he replies me with an empty result set, and I can’t figure out why.
The query is the following:
SELECT sons.*, (SELECT grandsons.* FROM default grandsons WHERE grandsons.parent_id = sons.objectId) AS nodes FROM default main
JOIN default sons ON KEYS main.nodes
WHERE main.type = 'root'
AND main.objectId = 'root::d6a89d80-e2da-11e4-908b-f18a9604f194'
group by sons
The purpose is to retrieve the first and second level nodes of a tree structured as follows:
Every node has an array field ‘nodes’, which contains the ids of his direct sons. Every node except the root has also a ‘parent_id’ field, that represents the id of the parent of this node in the tree structure.
NB: The query without the subquery returns a not-empty result set.
Have I misunderstood something on N1QL’s subqueries behavior?
[EDIT] Sorry for the inconvenience, I’ve forgotten that in subqueries with a from clause is mandatory to insert an USE KEYS clause! I restructured the query and now it works!