I run a query similar to this:
SELECT event.visitId, elementValue.dataOption as vdo FROM bucket event
UNNEST event.elementValues elementValue
WHERE event.`type` = 'EXAM'
AND elementValue.`type` = 'EXAM_TYPE'
EXCEPT
SELECT event.visitId, selectedValue as vdo FROM bucket event
UNNEST event.elementValues elementValue
UNNEST elementValue.selectedValues selectedValue
WHERE event.`type` = 'REQUEST'
AND elementValue.`type`= 'REQUEST_EXAMS'
And I would like to get the ids of the returned values but I can’t add it to the SELECT statement otherwhise the EXCEPT won’t work anymore. Is their any workaround?
Here are some sample documents (they have been handwritten for this use-case):
{
	"_id": "6a90946b-9f54-43e7-ab9a-9c9ac29b4e0e",
	"type": "EXAM",
	"elementValues": [
		{
			"type": "EXAM_VALUE"
			"value": "++"
		},
		{
			"type": "EXAM_TYPE"
			"dataOption": "TDR"
		}
	],
	"visitId": "0aa55d87-5a18-4b0e-b328-913479108bad"
},
{
	"_id": "45f13433-9e35-4ea1-a430-56de6a811a68",
	"type": "EXAM",
	"elementValues": [
		{
			"type": "EXAM_VALUE"
			"value": "O"
		},
		{
			"type": "EXAM_TYPE"
			"dataOption": "GR_SANG"
		}
	],
	"visitId": "0aa55d87-5a18-4b0e-b328-913479108bad"
},
{
	"_id": "72768a3e-d92c-4b65-a1ec-67a7527fc0ba",
	"type": "EXAM",
	"elementValues": [
		{
			"type": "EXAM_VALUE"
			"value": "--"
		},
		{
			"type": "EXAM_TYPE"
			"dataOption": "HEMO"
		}
	],
	"visitId": "0aa55d87-5a18-4b0e-b328-913479108bad"
},
{
	"_id": "965396be-5a4b-4de9-9366-94d9216e31ac",
	"type": "REQUEST",
	"elementValues": [
		{
			"type": "REQUEST_EXAMS"
			"selectedValues": [
				"TDR",
				"GR_SANG"
			]
		}
	],
	"visitId": "0aa55d87-5a18-4b0e-b328-913479108bad"
},
{
	"_id": "e2e9c817-1d8a-46b4-99b6-8f14f208e809",
	"type": "EXAM",
	"elementValues": [
		{
			"type": "EXAM_VALUE"
			"value": "++"
		},
		{
			"type": "EXAM_TYPE"
			"dataOption": "TDR"
		}
	],
	"visitId": "12d6c90e-d0b6-4f1f-a6a8-3a72acda4d82"
}
The document keys I would expect to receive are 72768a3e-d92c-4b65-a1ec-67a7527fc0ba and  e2e9c817-1d8a-46b4-99b6-8f14f208e809 because they aren’t any requests with the same visitId and a selectedValue containing the dataOption of the EXAM_TYPE elementValue.
Hope this is clear.
Many thanks.