mongodb - 在返回的查询文档中添加排名系统?
问题描述
我的用户集合包含以下文档:
{"_id" : 1, point_count: "12"},
{"_id" : 2, point_count: "19"},
{"_id" : 3, point_count: "13"},
{"_id" : 4, point_count: "1233"},
{"_id" : 5, point_count: "1"},
... and about 1000 more
我的问题是:当我按 id 搜索时,是否可以根据 point_count 字段显示每个用户的排名?假设我使用此查询查找 id = 4 的用户
db.User.find({_id: 4})
假设我整个收藏中的最高 point_count 是 1233,我希望得到这个结果
{"_id" : 4, point_count: "1233", rank: 1}
排名 1 因为 1233 是最高 point_count
或者当我搜索 id = 5 的用户时,我的预期结果应该是
{"_id" : 5, point_count: "1", rank: 1000..something}
1000 ..something 因为它是最低级别的,并且文档中有大约 1000+ id。非常感谢大家在这里帮助我!
解决方案
推荐阅读
- scala - Spark Json 迭代
- android - React Native geolcation 卫星数量
- r - ggplot() 和 geom_smooth() 的问题
- vb.net - 由于阴影,Me 关键字无法在派生类中执行子例程
- javascript - 如何以格式(YYYY-MM-DD)获取时刻格式日期选择器的值
- ios - 如何在 swift 5.1 中设置 UICollectionView 的标题大小?
- javascript - Redux React-Native(在 react-redux 中使用 useSelector 与使用 connect 有什么区别?)
- amazon-web-services - 我可以使用 Aws Cognito 通过 Twitter 进行身份验证吗?
- pandas - 无法将多索引数据框写入 excel
- javascript - type="number" 在 onChange 中反应打字稿误报?