0

current_userMongoid モデルがあり、モデルとリアルタイムで計算されたスコアによって結果を並べ替えたいと思います。インスタンス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

特定のユーザーに対して返された値でクエリの結果を並べ替えることができますか?

4

1 に答える 1

3

MongoDB は、並べ替えで任意の式をサポートしていません。基本的に、フィールドと方向 (asc/desc) のみを指定できます。

あなたのような複雑な並べ替えロジックでは、唯一の方法はアプリで行うことです。または、別のデータ ストアを調べます。

于 2012-07-02T08:33:41.580 に答える