Mongodb Mongoid查询!限制与顺序的结合
在我的rails应用程序中,我想抓取最后10条具有唯一值的记录 我试过:Mongodb Mongoid查询!限制与顺序的结合,mongodb,mongoid,Mongodb,Mongoid,在我的rails应用程序中,我想抓取最后10条具有唯一值的记录 我试过: @users = User.all(:limit => 10, :sort => [:created_at, :desc]).map{|t| t.first_name}.uniq 但是, 这个限制没有我想象的那么有效 我希望“限制”最后影响查询 有什么想法吗 @users = User.desc(:created_at).limit(10).map(&:first_name).uniq 您应该使用d
@users = User.all(:limit => 10, :sort => [:created_at, :desc]).map{|t| t.first_name}.uniq
但是,
这个限制没有我想象的那么有效
我希望“限制”最后影响查询
有什么想法吗
@users = User.desc(:created_at).limit(10).map(&:first_name).uniq
您应该使用distinct
。uniq
可能会导致您获得的记录少于10条
@users = User.limit(10).distinct(:first_name).desc(:created_at).map(&:first_name)
事实上,问题是我在uniq的成绩不到10分。不幸的是,第二行给出了[]的未定义方法“limit”:Array@vic:方法的顺序可能是错误的。我现在不能考试。尝试编辑的版本。看起来@users=User.limit(10).desc(:created_at).distinct(:first_name)几乎可以工作!但是忽略排序顺序“desc”…也尝试了排序方式([:created_at,:desc])…但是没有排序..谢谢您的时间!