如何获得行的等级/行号? [英] How do I get the rank / row number for a row?
问题描述
是否可以获取过滤集的行号(行)并将行号附加到结果中?
Is there a way to get the row numbers (rank) for a filtered set and append the row number to the result?
一个示例场景是我有一个表,记录如下:
An example scenario would be that I have a table with records like:
[
{ points: 123, name: 'Glenn' },
{ points: 948, name: 'Bob' },
{ points: 22, name: 'Sarah' }
]
在上表中,有成千上万的行,我希望能够根据点下降等条件对所有记录进行排名,然后返回行的子集(使用过滤器),其中包括其排名值结果如下:
In the above table there are hundreds of thousands of rows, and I want to be able to rank all records based on a condition like points descending and then return a subset of the rows (using a filter) with their rank value included in the result like this:
[ { points: 123, name: 'Glenn', rank: 2 }]
推荐答案
您应该使用offsetsOf
函数
r.table('users')
.orderBy({index: 'points'})
.offsetsOf(r.row('user_id').eq(yourUserId))
.run(conn, callback)
r.table('users')
.orderBy({index: 'points'})
.offsetsOf(r.row('user_id').eq(yourUserId))
.run(conn, callback)
命令参考: http://rethinkdb.com/api/javascript/offsets_of/
这篇关于如何获得行的等级/行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!