ID: MSGPQ:201:E81863521C48:2020-10-29
{
"propCode": "MSGPQ",
"roomNumber": "201",
"macAddress": "E81863521C48",
"date": "2020-10-29",
"source": "linearTv",
"lastReportedTime": 1603227647,
"channels": [
{
"channelName": "HBO",
"channelId": 22,
"sessions": [
{
"isOccupied": 1,
"startTime": 1603227747,
"stopTime": 1603227847,
"duration": 665,
"created": 1603227747,
"tunerDetails": [
{
"created": 1603228647,
"signalLocked": 1,
"signalLevel": 56,
"signalQuality": 28,
"errorLevel": 0
},
{
"created": 1603229647,
"signalLocked": 0,
"signalLevel": 56,
"signalQuality": 28,
"errorLevel": 0
}
]
},
{
"isOccupied": 0,
"startTime": 1603227648,
"stopTime": 1603227649,
"duration": 0,
"created": 1603227648,
"tunerDetails": [
{
"created": 1603227657,
"signalLocked": 0,
"signalLevel": 56,
"signalQuality": 28,
"errorLevel": 0
},
{
"created": 1603227667,
"signalLocked": 0,
"signalLevel": 56,
"signalQuality": 28,
"errorLevel": 0
}
]
}
]
},
{
"channelName": "ESPN",
"channelId": 850,
"sessions": [
{
"isOccupied": 0,
"startTime": 1603227747,
"stopTime": 1603227847,
"duration": 665,
"created": 1603227747,
"tunerDetails": [
{
"created": 1603228647,
"signalLocked": 0,
"signalLevel": 6,
"signalQuality": 8,
"errorLevel": 0
},
{
"created": 1603229647,
"signalLocked": 1,
"signalLevel": 6,
"signalQuality": 2,
"errorLevel": 0
}
]
}
]
}
]
}
Provided the single document above, I have a query to populate the channels array with the matching channels. The channels array contains two channels and I return one based on the channelName and channelId.
SELECT document.*,
ARRAY c FOR c IN document.channels WHEN c.channelName = "HBO" AND c.channelId = 22 END AS channels
FROM connectedroom.ecmp.tvSessions AS document
WHERE document.date BETWEEN "2020-10-20" AND "2020-10-30"
AND document.source = "linearTv"
AND document.propCode = "MSGPQ"
AND ANY c IN document.channels SATISFIES c.channelName = "HBO" AND c.channelId = 22 END
ORDER BY document.date DESC;
Output:
[
{
"channels": [
{
"channelId": 22,
"channelName": "HBO",
"sessions": [
{
"created": 1603227747,
"duration": 665,
"isOccupied": 1,
"startTime": 1603227747,
"stopTime": 1603227847,
"tunerDetails": [
{
"created": 1603228647,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 1,
"signalQuality": 28
},
{
"created": 1603229647,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 0,
"signalQuality": 28
}
]
},
{
"created": 1603227648,
"duration": 0,
"isOccupied": 0,
"startTime": 1603227648,
"stopTime": 1603227649,
"tunerDetails": [
{
"created": 1603227657,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 0,
"signalQuality": 28
},
{
"created": 1603227667,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 0,
"signalQuality": 28
}
]
}
]
}
],
"date": "2020-10-29",
"lastReportedTime": 1603227647,
"macAddress": "E81863521C48",
"propCode": "MSGPQ",
"roomNumber": "201",
"source": "linearTv"
}
]
Now, I would like to go one level deeper into the sessions array. I would like to match on the sessions.duration field and only return sessions with duration > 0.
The expected output would be:
[
{
"channels": [
{
"channelId": 22,
"channelName": "HBO",
"sessions": [
{
"created": 1603227747,
"duration": 665,
"isOccupied": 1,
"startTime": 1603227747,
"stopTime": 1603227847,
"tunerDetails": [
{
"created": 1603228647,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 1,
"signalQuality": 28
},
{
"created": 1603229647,
"errorLevel": 0,
"signalLevel": 56,
"signalLocked": 0,
"signalQuality": 28
}
]
}
]
}
],
"date": "2020-10-29",
"lastReportedTime": 1603227647,
"macAddress": "E81863521C48",
"propCode": "MSGPQ",
"roomNumber": "201",
"source": "linearTv"
}
]
Current query:
SELECT document.*,
ARRAY c FOR c IN document.channels WHEN c.channelName = "HBO" AND c.channelId = 22
AND (ARRAY s FOR s IN c.sessions WHEN s.duration > 0 END) END AS channels
FROM connectedroom.ecmp.tvSessions AS document
WHERE document.date BETWEEN "2020-10-20" AND "2020-10-30"
AND document.source = "linearTv"
AND document.propCode = "MSGPQ"
AND ANY c IN document.channels SATISFIES c.channelName = "HBO" AND c.channelId = 22 END
AND ANY s IN document.channels.sessions SATISFIES s.duration > 0 END
ORDER BY document.date DESC;
Current results:
{
"results": []
}
I’m unsure my approach is correct and I’m struggling to build the nested arrays in the result. From my understanding, the SELECT clause builds the result document?