javascript - 聚合排序后如何找出项目的索引?
问题描述
我希望能够在其输出中显示项目的索引。
这是我现在的代码:
Picks.aggregate([
{
"$group": {
_id: "$user.uid",
name: {
$first: "$user.name"
},
total: {
$sum: "$total"
},
}
},
{
"$sort": {
"total": -1
}
},
{
"$limit": 10
}
])
我对使用聚合没有任何问题,我可以对需要的字段进行分组。这是一个示例输出,我已经注释掉了我正在寻找的内容。
[
{
_id: "1a2b3c4d5e6f7g8h9i",
name: "Jane Doe",
total: 7384,
// next line is what I'm looking for
// rank: 0 <-- it's the first item in an array after $sort
},
{
_id: "a1b2c3d4e5f6g7h8i9",
name: "John Doe",
total: 7370,
// next line is what I'm looking for
// rank: 1 <-- it's the second item in an array after $sort
},
...
]
解决方案
推荐阅读
- flutter - 当用户输入正确长度时如何使后缀图标标记为绿色,否则它将显示红色十字图标
- html - 检查无线电输入未触发:检查伪选择器
- c++ - 我们如何使用互斥锁或信号量来实现进程同步?
- performance - 为什么只索引一个文档需要这么长时间
- electron - Electron 如何找出哪个进程正在执行公共模块?
- javascript - 在 forEach 循环中使用 onclick 将变量传递给函数 - Google Apps 脚本
- c++ - C++ 基类作为代理
- c# - “程序不包含适用于入口点 WindowsFormsApp 的静态 'Main' 方法”错误消息
- php - 如何在 For 和 while 循环中像先进先出一样在 Top 位置添加数据?可能是反向循环?
- c# - 从资产文件夹统一加载精灵到游戏对象