0

像这样的集合“问题”

{
     _id:
     question:
     time:
     click_nums:
     answers: [
        {},
        {},
        ...
     ]
}

现在我想像这样对问题进行排名并获得前 5 名:

 rank_condition = (now_time - time)/24*(click_nums*0.2+nums_of_answers*0.8)

我怎么能意识到这一点,我是 Mongodb 的新手..

空间:用PHP来实现!!!

4

1 回答 1

2

MongoDB can (currently) sort only on a field's value. It doesn't support complex expressions (as in SQL's ORDER BY). The new Aggregation Framework can indeed be of some help here.

You have three options:

  1. Rank in the application. This is the easiest to implement.

  2. Precalculate ranking position (run periodic background jobs that calculate and update ranks). This is the most efficient method to query (you might get slightly stale data).

  3. Use Aggregation Framework (available in 2.2 and current unstable). This is the "coolest" method. (you get to play with new toys :) )

于 2012-04-09T09:08:13.780 回答