我有一个对象报告包含两个整数字段:月和年。 我需要通过 “日期”由两个字段mongoid排序
Report.desc(:year).desc(:month).each do |a|
puts a.year.to_s + " " + a.month.to_s
end
结果对它进行排序:
2011 12
2011 11
2012 7
2012 6
2012 5
2012 4
2012 3
2012 2
2012 1
虽然我觉得让
2012 7
2012 6
2012 5
2012 4
2012 3
2012 2
2012 1
2011 12
2011 11
我在做什么错?
的Mongoid标准是这样的:
irb(main):043:0> Report.desc(:year).desc(:month)
=> #<Mongoid::Criteria
selector: {},
options: {:sort=>{"year"=>-1, "month"=>-1}},
class: Report,
embedded: true>
你试过'.desc(:year,:month)'吗?阅读[原始文档](http://mongoid.org/en/origin/docs/options.html#sorting)让我认为这是你需要的。这假定你正在使用Mongoid 3.我认为可能发生的事情是你的第二个'desc'调用覆盖你的第一个。 Mongoid从你的整个搜索中构建一个单一的查询,所以它不应该一个接一个地执行。虽然这并不符合你的标准,但这很奇怪。 – 2012-07-31 20:11:37