I m actually having trouble when dealing with MongoDb.
I need to :
$last
gives you the value of the last document which is processed by your grouping. You can not predict the order in which documents are processed, unless you sort the documents. So you don't get the results you expect.
Try adding a $sort stage before your $group stage to get the documents in ascending date order:
db.collection.aggregate(
[
{ $sort: {
"control_date":1
}
},
{
$group:
{
_id: "$zone._id",
lastRegistered: { $last: "$_id" },
zoneName:"$zone.zoneName",
control_id:"$_id",
actif:"$actif",
}
}
]
)