Starting in Mongo 5.2
, it's a perfect use case for the new $topN
aggregation accumulator:
// { blog_id: "a", title: "plop", content: "smthg" }
// { blog_id: "b", title: "hum", content: "meh" }
// { blog_id: "a", title: "hello", content: "world" }
// { blog_id: "a", title: "what", content: "ever" }
db.collection.aggregate([
{ $group: {
_id: "$blog_id",
messages: { $topN: { n: 2, sortBy: { _id: -1 }, output: "$$ROOT" } }
}}
])
// {
// _id: "a",
// messages: [
// { blog_id: "a", title: "what", content: "ever" },
// { blog_id: "a", title: "hello", content: "world" }
// ]
// }
// {
// _id: "b",
// messages: [
// { blog_id: "b", title: "hum", content: "meh" }
// ]
// }
This applies a $topN
group accumulation that:
- takes for each group the top 2 (
n: 2
) elements
- top 2, as defined by
sortBy: { _id: -1 }
, which in this case means by reversed order of insertion
- and for each record pushes the whole record in the group's list (
output: "$$ROOT"
) since $$ROOT
represents the whole document being processed.