我有一個對象報告包含兩個整數字段:月和年。 我需要通過 「日期」由兩個字段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