For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

$top (accumulator operator)

$top

Returns the top element within a group according to the specified sort order.

Note

Disambiguation

This page describes $top when used as an accumulator. Accumulators return a single aggregated value across a group of input documents.

You can also use $top in these other contexts:

{
$top:
{
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
Field
Necessity
Description

sortBy

Required

Specifies the order of results, with syntax similar to $sort.

output

Required

Represents the output for each element in the group and can be any expression.

Consider the following aggregation that returns the top document from a group of scores:

  • $top does not filter out null values.

  • $top converts missing values to null.

db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: 2 },
{ playerId: "PlayerC", gameId: "G1", score: 3 },
{ playerId: "PlayerD", gameId: "G1"},
{ playerId: "PlayerE", gameId: "G1", score: null }
]
},
{
$group:
{
_id: "$gameId",
playerId:
{
$top:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": 1 }
}
}
}
}
] )

In this example:

  • $documents creates the literal documents that contain player scores.

  • $group groups the documents by gameId. This example has only one gameId, G1.

  • PlayerD has a missing score and PlayerE has a null score. These values are both considered as null.

  • The playerId and score fields are specified as output : ["$playerId"," $score"] and returned as array values.

  • Specify the sort order with sortBy: { "score": 1 }.

  • PlayerD and PlayerE tied for the top element. PlayerD is returned as the top score.

  • To have more deterministic tie breaking behavior for multiple null values, add more fields to sortBy.

When sorting different types, the order of BSON data types is used to determine ordering. As an example, consider a collection whose values consist of strings and numbers.

  • In an ascending sort, string values are sorted after numeric values.

  • In a descending sort, string values are sorted before numeric values.

In this example:

  • PlayerA has an integer score.

  • PlayerB has a string "2" score.

  • PlayerC has an empty string score.

Because the sort is in descending { "score": -1 }, string values are sorted before PlayerA's numeric score. $top returns the first element after sorting, which is PlayerB:

db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: "2" },
{ playerId: "PlayerC", gameId: "G1", score: "" }
]
},
{
$group:
{
_id: "$gameId",
playerId:
{
$top:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )

The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

You can use the $top accumulator to find the highest-rated movie in a genre.

db.movies.aggregate( [
{
$match: {
genres: "Comedy",
"imdb.rating": { $gt: 0 }
}
},
{
$group:
{
_id: "Comedy",
highestRatedMovie:
{
$top:
{
output: [ "$title", "$imdb.rating" ],
sortBy: { "imdb.rating": -1 }
}
}
}
}
] )

The example pipeline:

  • Uses $match to filter for Comedy movies with a positive IMDb rating.

  • Uses $group to group all Comedy movies under a single "Comedy" group key.

  • Specifies the fields that are output for $top with output : ["$title", "$imdb.rating"].

  • Uses sortBy: { "imdb.rating": -1 } to rank movies within the group by IMDb rating in descending order, which determines the movie that $top returns.

You can use the $top accumulator to find the highest-rated movie for each movie rating category.

db.movies.aggregate( [
{
$match: {
rated: { $in: [ "G", "PG", "PG-13", "R" ] },
"imdb.rating": { $gt: 0 }
}
},
{
$group:
{
_id: "$rated",
highestRatedMovie:
{
$top:
{
output: [ "$title", "$imdb.rating" ],
sortBy: { "imdb.rating": -1 }
}
}
}
},
{
$sort: { _id: 1 }
}
] )

The example pipeline:

  • Uses $match to filter for movies in the G, PG, PG-13, and R categories with a positive IMDb rating.

  • Uses $group to group the results by rated.

  • Uses $top to return the highest-rated movie for each category.

  • Specifies the fields that are output for $top with output : ["$title", "$imdb.rating"].

  • Uses sortBy: { "imdb.rating": -1 } to rank movies within each group by IMDb rating in descending order, which determines the movie that $top returns.

  • Uses $sort to sort the results alphabetically by rating category.