Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 297
cortex Avatar asked May 23 '13 17:05

cortex


People also ask

How can I see when a document was last updated in MongoDB?

To get last inserted document, use sort() along with limit(1).


1 Answers

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.

like image 118
cortex Avatar answered Oct 11 '22 19:10

cortex