【发布时间】:2012-02-03 21:37:55
【问题描述】:
使用 MongoDB 和 Ruby 驱动程序,我正在尝试计算我的应用中玩家的排名,因此我按(在本例中)俯卧撑进行排序,然后为每个对象添加排名字段和值。
pushups = coll.find.sort(["pushups", -1] )
pushups.each_with_index do |r, idx|
r[:pushups_rank] = idx + 1
coll.update( {:id => r }, r, :upsert => true)
coll.save(r)
end
这种方法确实有效,但这是迭代对象并更新每个对象的最佳方法吗?有没有更好的方法来计算玩家的排名?
【问题讨论】: