Probeer het met het volgende fragment
db.collection.aggregate([
{$group: {
"_id": "$device_id",
"gateway_id": {"$last":"$gateway_id"},
data: {$last: '$data'},
date: {$last: '$date_time'},
}},
{$project: {
"device_id": "$_id",
"gateway_id": "$gateway_id",
"data": "$data",
"date_time": "$date"
}},
{$sort: {
'date': -1
}}
])
In bovenstaande zoekgroep op apparaat-ID en datum, gegevens en gateway_id zijn de laatste in elke rij.
Uitgang is-
{
"result" : [
{
"_id" : 29,
"gateway_id" : 1,
"data" : [
{
"r" : 203,
"v" : 3002
},
{
"r" : 221,
"v" : 3006
}
],
"device_id" : 29,
"date_time" : "a"
},
{
"_id" : 28,
"gateway_id" : 1,
"data" : [
{
"r" : 203,
"v" : 3002
},
{
"r" : 221,
"v" : 3006
}
],
"device_id" : 28,
"date_time" : "b"
},
{
"_id" : 27,
"gateway_id" : 1,
"data" : [
{
"r" : 203,
"v" : 3642
},
{
"r" : 221,
"v" : 3666
}
],
"device_id" : 27,
"date_time" : "a"
}
],
"ok" : 1
}
Bedankt