通过动态的,特定于用户的值来订购Mongoid结果

时间:2012-07-02 07:42:05

标签: ruby-on-rails mongodb mongoid database

我有一个Mongoid模型,我想按照模型和current_user之间的实时分数来计算结果。假设Thing具有实例方法match_score

  def match_score(user) #can be user object too
    score = 100
    score -= 5 if user.min_price && !(price < user.min_price)
    score -= 10 if user.max_price && !(price > user.max_price)
    #... bunch of other factors...
    return [score, 0].max
  end

是否可以按特定用户返回的值对任何查询的结果进行排序?

1 个答案:

答案 0 :(得分:3)

MongoDB在排序时不支持任意表达式。基本上,您只能指定字段和方向(asc / desc)。

使用如此复杂的排序逻辑,唯一的方法是在应用程序中执行此操作。或者查看另一个数据存储。

相关问题