Hi! I’m trying to perform a simple lookup stage, but the data in the “foreign” collection is quite big. Is there a way to limit that incoming data, so that I can get only one, or preferably, the latest record according to a field called timestamp specified in the incoming documents?
While using the aggregation pipeline, when you have result with the $lookup and $match, there can be two ways to display the most recent data or get only one data from all the documents of the resultant query.
Using a $sort with $first to sort the data based on the ObjectId field.
P.S. The $first works is only meaningful when documents are in a defined order.
$limit would also help in your case where you would want to display only the one response of the output documents.