I am working on an application that shows a table of aggregate data I sum, part of the dashboard I want to show a sample of the last few events that came into the collection:
I am executing this pipeline but it crashes OOM because I have millions of documents in the collection, is there anything I can do? I want to get the last 5 texts
Events.aggregate([
{
$match: {event_type: {$in: [1,5,10,12]}}
},
{
$group: {
_id: "$event_type", avgTime: {$avg: "$time"}, avgCost: {$avg: "$cost"}, maxCost: {$max: "$cost"}, maxTime: {$max: "$time"}, texts: {$push: "$text"}
}
},
{
$addFields: {
texts: { $slice: ["$text", {$subtract: [{$size: "$text"}, 5]}, {$size: "$text"}]}
}
}
])