質問

I’d like to sort by date then by rating but I’d like the date rounded to the nearest half year (or some time span).

This would have the effect of showing most recent 6 months ordered by rating, then next most recent 6 months ordered by rating.

Currently I have a scope:

scope :newer_popular, order_by(:release_date => :desc, :avg_rating => :desc).where(:release_date.ne => 0, :avg_rating.ne => 0 )

Obviously doesn't do it, this treats the release_date (a Date object) as a continuous ordering so only objects that happened to have been released at exactly the same time are actually ordered by rating.

Is there a way to do this faceted/grouped/rounded ordering in Mongoid?

役に立ちましたか?

解決

I would order just by rating, and use a where criteria for release date:

where(:release_date.gte => 6.months.ago.utc).order_by(:avg_rating => :desc)

For the next page:

where(:release_date.lt => 6.months.ago.utc, :release_date.gte => 12.months.ago.utc).order_by(:avg_rating => :desc)

Or something like that.

他のヒント

If you are displaying results without data reduction, then it is easiest to fetch the desired data and do your programming in Ruby, that's why we love it so.

If you want to do some data reduction or learn about server-side processing, then you can consider the aggregation framework for manipulation and grouping that extends well beyond the standard MongoDB query language.

I've constructed aggregation pipelines for other SO answers. Let me know if you want an answer for this, but might take me some time to get around to it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top