How to limit aggregation match results to last n documents?

> db.getCollection("daily_signals").aggregate(
>         [
>     
>         {
>             $match: {
>                 "Stock":{$in: ['SENSOR1','SENSOR2','SENSOR15']},
>                 $or: [
>                     {
>                         "MOSC": {
>                             $gt: 0.0
>                         }
>                     },
>                     {
>                         "SHL": {
>                             $gt: 0.0
>                         }
>                     },
>                     {
>                         "DDSM": {
>                             $gt: 0.0
>                         }
>                     }
>                 ],
>             },
> 
>         },
>        
>         ])

With this aggregation I’m able to query previous signals (where MOSC,SHL,DDSM >1) for each sensor.
However this query returns all signals for each sensor. I would like to get only 2 latest signals of each sensor.(last 2 documents). I tried to limit after match. and also wrap with facet.
Here is an example document:

> {
>     "_id" : ObjectId("64adb9af35dbc22c4b376cf8"),
>     "Date" : ISODate("2023-07-11T00:00:00.000+0000"),
>     "Sensor" : "SENSOR15",
>     "MOSC" : NumberInt(66),
>     "SHL" : NumberInt(0),
>     "DDSM" : NumberInt(0),
> }

Thanks in advance.

Could you please show me an example of how i can apply $limitN to this aggregation?

If you sort then group and use the lastN group operator as @Jack_Woehr said above, you can then unwind and get the documents, something like this:

db.getCollection("Test").aggregate([
{
    $sort:{
        SensorName:1,
        TimeSequence:1
    }
},
{
    $group:{
        _id:'$SensorName',
        lastData:{
            $lastN:{
                input:'$$ROOT',
                n:2
            }
        }
    }
},
{
    $unwind:'$lastData'
},
{
    $replaceRoot:{
        newRoot:'$lastData'
    }
}
])

You could probably also do it with the window operator: