- Reference >
- mongo Shell Methods >
- Cursor Methods >
- cursor.skip()
cursor.skip()¶
- cursor.skip()¶
Call the cursor.skip() method on a cursor to control where MongoDB begins returning results. This approach may be useful in implementing “paged” results.
Note
You must apply cursor.skip() to the cursor before retrieving any documents from the database.
Consider the following JavaScript function as an example of the skip function:
function printStudents(pageNumber, nPerPage) { print("Page: " + pageNumber); db.students.find().skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } ); }
The cursor.skip() method is often expensive because it requires the server to walk from the beginning of the collection or index to get the offset or skip position before beginning to return results. As the offset (e.g. pageNumber above) increases, cursor.skip() will become slower and more CPU intensive. With larger collections, cursor.skip() may become IO bound.
Consider using range-based pagination for these kinds of tasks. That is, query for a range of objects, using logic within the application to determine the pagination rather than the database itself. This approach features better index utilization, if you do not need to easily jump to a specific page.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.