I am creating a program that uses mongodb to save and retrieve game values, but I need my program to retrieve the latest document in a timeseries collection (and I also need it to automatically update when a document is added, but that doesnt matter right now).
But because all the data in a document is more or less random I dont think I could use $match to find the most recent document,
Here is an example document:
{"timestamp":{"$date":{"$numberLong":"1693107280000"}},"gameNumber":"105","headTailResult":"Heads","_id":{"$oid":"64eac45dcd8bc390a8a33d14"},"drawNumbers":"1, 4, 7, 13, 17, 22, 23, 25, 26, 28, 34, 45, 46, 47, 53, 57, 66, 67, 70, 72","multiplier":"1","gameTime":"2023-08-27 03:34:40"}
Things to note: gameNumber goes up to 999 then back to 000, multiplier can range from 1-5, 10
I came up with this aggregate command based on the documentation here, but it doesnt actually return anything only “undefined”
var gameResult = gameCollection.aggregate([
{
$sort: {timestamp: -1}
}
,
{
$limit: 1
}
])
Im guessing its because I havent specfied a $match so it doesnt return anything, but nothing will match I only want the most recent. Can anyone help?