Search code examples
rubymongodbmongoid

How to get last N documents with mongoid?


I have found some information to accomplish this in mongoDB, but I need it with mongoid. So I can do something like:

User.last(7000).each do ....

I'm using:

  • MongoDB shell version: 2.4.3

  • Mongoid 2.6.0

Thanks!


Solution

  • Now I found a solution from mongoid origin:

    User.all.desc('_id').limit(7000)
    

    It sorts the users in descending order according to the id.