I am using Golang and the MGO library
I have some test records where I want to GROUP BY the serial number, stage, stage order and grab the last record by the datetime field. Most instances there is 1 record that per serial/stage/stage order but there can also be instances where there are multiple tests and I would like to grab the last test completed, not all of them for that combination.
So, in short, there are records in my table that will have the same serial, stage, and stage order but different timestamps, I want to grab the last or only record for the entire data set so that I have a single record for every (serial, stage, stage order) combination and if there were duplicate tests I always grab the last record in that group.
With the above example I ONLY want to get the 2nd record back for that serial.
My Code: I was trying to create a pipeline but can't seem to get it setup correctly:
pipeline := []bson.M{
bson.M{
"$match": bson.M{"workorder": i},
},
bson.M{
"$group": bson.M{
"_id": "id",
"serial": bson.M{"$match": "$serial"},
"stage": bson.M{"$match": "$stage"},
"order": bson.M{"$match": "$order"},
"date": bson.M{"$last": "$date_timestamp"},
},
},
}