简体   繁体   English

Mongodb获取文档在集合中的位置

[英]Mongodb get document position in collection

Is there a way to get a document's position relative to its collection based on one of its properties in Mongodb? 有没有一种方法可以根据其在Mongodb中的属性之一来获取文档相对于其集合的位置?

The use case is that I am building a leaderboard, but want to provide an easy way for the user to know their rank without having to scroll through all the entries. 用例是我正在建立一个排行榜,但希望为用户提供一种无需滚动所有条目即可知道其排名的简便方法。

Is there some mongoose schema magic or mongodb query that will help me easily get the position of a user based on their score? 是否存在一些猫鼬模式魔术或mongodb查询,这些查询可以帮助我根据他们的得分轻松获得用户的位置?

Currently, my solution is to create an index on the score, query the entire collection and find the index of the user in the result. 当前,我的解决方案是在得分上创建索引,查询整个集合并在结果中找到用户的索引。

Strictly speaking, this is not possible, as MongoDB does not store its documents in any certain order. 严格来说,这是不可能的,因为MongoDB不会以任何特定顺序存储其文档。 Your current approach should be fine. 您当前的方法应该没问题。

You're not the first one with this question: there's a feature request for this exact thing with a high priority. 您不是第一个提出此问题的人:对此功能有很高的优先级的功能要求。

https://jira.mongodb.org/browse/SERVER-4588 https://jira.mongodb.org/browse/SERVER-4588

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM