I'm trying to create a N1QL query that would return a list of clients and embed as well an array of API keys. Here is my setup:
Bucket:
{
"Client-a3c1c7f8r89732": {
"type": "Client",
"id": "a3c1c7f8r89732",
"name": "Cognito Inc"
},
"Client-z6cc6f4r6feer5": {
"type": "Client",
"id": "z6cc6f4r6feer5",
"name": "Some Corp"
},
"ApiKey-3846516546897987": {
"type": "ApiKey",
"key": "3846516546897987",
"client_id": "a3c1c7f8r89732"
},
"ApiKey-98765164865351321": {
"type": "ApiKey",
"key": "98765164865351321",
"client_id": "a3c1c7f8r89732"
},
"ApiKey-646546846546549887": {
"type": "ApiKey",
"key": "646546846546549887",
"client_id": "a3c1c7f8r89732"
}
}
Indexes available:
CREATE INDEX `idx-client-query` ON `primary`(`type`,`name`,`id`) WHERE (`type` = "Client")
CREATE INDEX `idx-apikey-query` ON `primary`(`type`,`client_id`,`key`) WHERE (`type` = "ApiKey")
Desired N1QL query output:
[
{
"id": "a3c1c7f8r89732",
"name": "Cognito Inc",
"api_keys": [
{
"key": "3846516546897987"
},
{
"key": "98765164865351321"
},
{
"key": "646546846546549887"
}
]
},
{
"id": "z6cc6f4r6feer5",
"name": "Some Corp",
"api_keys": []
}
]
So far I've tried
- To use an ANSI RIGHT OUTER JOIN to retrieve clients who do not have api keys (the query result would need to be reprocessed to nest the keys). For some reason, it only retrieves the clients that do have keys.
- To use an ANSI NEST such as below but I simply get no results
SELECT client.name, client.id, api_key.* as api_keys
FROM `primary` client
INNER NEST `primary` api_key ON client.id = api_key.client_id
WHERE client.type = 'Client' AND api_key.type = 'ApiKey'
Any help or link to a similar problem would be much appreciated, thanks :)