Returns the value that results from applying an expression to the last document in a group of documents that share the same group by a field. Only meaningful when documents are in a defined order.
$lasthas the following syntax:
For more information on expressions, see Expressions.
sales collection with the following documents:
The following operation first sorts the documents by
date, and then in the following
$group stage, groups the now sorted documents by the
item field and uses the
$last accumulator to compute the last sales date for each item:
The operation returns the following results: