I have a collection of documents of the format:
{
"user": { "username": "string" }
"score": 123
}
I want to find a user's rank within the collection.
collection.aggregate([
{ $sort: { score: -1 } }, // sort by score descending
{
$project: {
score: '$score'
rank: {
$indexOfArray: [
'$', // I don't know what to place here
{ 'user.username': 'myUser' }
]
}
}
}
]);
How do I treat the collection as an array?
I'm looking for this result:
[
{
"score": 123,
"rank": 1
}
]