I have a collection that contains time-based metrics. I only store them if they change over time and I want to keep their previous value in the aggregation result.
Here's an extract of the collection :
{
"_id": ObjectId("6115150f01d7d0426bcd0390"),
"conf": "conference123",
"uid": "2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"log": [
{
"dt": ISODate("2021-08-12T12:33:49.782Z"),
"connection_quality": 60,
"video_bitrate": 150
},
{
"dt": ISODate("2021-08-12T12:34:19.781Z"),
"video_bitrate": 145
// connection_quality didn't change so it's not stored
},
{
"dt": ISODate("2021-08-12T12:34:30.781Z"),
"video_bitrate": 130
// connection_quality didn't change so it's not stored
},
{
"dt": ISODate("2021-08-12T12:34:49.787Z"),
"connection_quality": 100,
"video_bitrate": 150
},
{
"dt": ISODate("2021-08-12T12:35:19.789Z"),
"video_bitrate": 160
// connection_quality didn't change so it's not stored
}
]
}
I tried the following aggregation but I don't know what to put after the last stage :
[{
$match: {
conf: 'conference123',
uid: '2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642'
}
}, {
$unwind: {
path: '$log'
}
}, {
$project: {
_id: 0,
"Date": '$log.dt',
'User ID': '$uid',
'Connection Quality': "$log.cq"
}
}]
Here's the result that I get
[
{
"Date": ISODate("2021-08-12T12:33:49.782Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":60
},
{
"Date": ISODate("2021-08-12T12:34:19.781Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642"
},
{
"Date": ISODate("2021-08-12T12:34:30.781Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642"
},
{
"Date": ISODate("2021-08-12T12:34:49.787Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":100
},
{
"Date": ISODate("2021-08-12T12:35:19.789Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642"
}
]
But this is what I want to display
[
{
"Date": ISODate("2021-08-12T12:33:49.782Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":60
},
{
"Date": ISODate("2021-08-12T12:34:19.781Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":60
},
{
"Date": ISODate("2021-08-12T12:34:30.781Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":60
},
{
"Date": ISODate("2021-08-12T12:34:49.787Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":100
},
{
"Date": ISODate("2021-08-12T12:35:19.789Z"),
"User ID":"2dd8b4e3-9dcd-4da6-bc36-aa0988dc9642",
"Connection Quality":100
}
]
Any help would be greatly appreciated, thanks !