Hot to get last 30 records of collection result in mongodb, but not from zero?


I try to make this with .sort({_id:1}).limit(30) but in result they are showing from 0 to 30, but not from 40 to 70 (for example), that's mean i see all messages to 30, and newer than 30 - not showing. How i can find out actually 30 messages from db.collection('messages') ?


You can set the first document to be included in the results by calling <a href="http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#skip" rel="nofollow">skip()</a> on the cursor:

.sort({_id: 1}).skip(40).limit(30)

Would provide the 41 through 70 documents after sorting by _id.

If you want to get the <em>last</em> 30 while keeping them in ascending order, reverse your sort, and then reverse the results using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse" rel="nofollow">Array#reverse</a>:

coll.find().sort({_id: -1}).limit(30).toArray((err, docs) => { docs.reverse(); });


  • Android CalendarContract, deleting a recurring event causes all events to disappear on calendar?
  • FindAndModify, return array of Objects
  • Mongodb exception, “ MongoCursorException' with message '$ operator made object too large”
  • iOS Localization Doesn't Work with More Than 63 Files
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Where these are stored?
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • how to set variables in a php include file?
  • Eloquent paginate function in Slim 3 project using twig
  • Is playing sound in Javascript performance heavy?
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • MongoError: Incorrect arguments
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Insert into database using onclick function
  • Using $this when not in object context
  • QLineEdit password safety
  • Knitr HTML Loop - Some HTML output, some R output
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Change an a tag attribute in JavaScript based on screen width
  • How to limit post in wp_query
  • Delete MySQLi record without showing the id in the URL
  • Why winpcap requires both .lib and .dll to run?
  • retrieve vertices with no linked edge in arangodb
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?