I have the following code from Compass on an pipeline aggregation that I’m looking to create. I would like to be able to rank by goals scored, but want to sort the data so that the best possible stats appear in a rank (1 being the lowest and somewhere near 100 being the highest) for players. The issue is, with the code below, the $setWindowFields stage needs the sortby command in order to get ranking and it basically throws away my previous sorting for the final 100 documents. Is there a way to get the ranking I need by more than 1 field?
[
{
$match:
/**
* query: The query in MQL.
*/
{
League: "English Premier League",
Pos: {
$in: ["MF", "MFFW"],
},
Min: {
$gt: 0,
},
Gls: {
$gt: 0,
},
},
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
Gls: -1,
},
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
100,
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
Gls: 1,
Gls_90: 1,
Ast: 1,
},
},
{
$setWindowFields:
/**
* partitionBy: partitioning of data.
* sortBy: fields to sort by.
* output: {
* path: {
* function: The window function to compute over the given window.
* window: {
* documents: A number of documents before and after the current document.
* range: A range of possible values around the value in the current document's sortBy field.
* unit: Specifies the units for the window bounds.
* }
* }
* }
*/
{
sortBy: {
Gls: 1,
},
output: {
rank: {
$documentNumber: {},
},
},
},
},
{
$project:
/**
* specifications: The fields to
* include or exclude.
*/
{
Player: "$Player",
Club: "$Club",
League: "$League",
Season: "$Season",
Gls_score: "$rank",
},
},
{
$out:
/**
* Provide the name of the output collection.
*/
"Rankings",
},
]