Ticket: Paging Chapter 2 Lesson: User Facing Backend

Hi Kanika,
I don’t understand why the code below works. The answer is on the bottom. I thought the answer would some type of iterative loop. Can you explain how the skip method results in a new page that lists further query search results? Thanks.


try {
cursor = await movies
.find(query)
.project(project)
.sort(sort)
} catch (e) {
console.error(Unable to issue find command, ${e})
return { moviesList: , totalNumMovies: 0 }
}

// here’s where paging is implemented
const displayCursor = cursor.skip(moviesPerPage * page).limit(moviesPerPage)

Hi @Jai_33176,

I am not sure if I understood your question right. If you meant to ask how skip operator results in a new page, then:

For every page, this function will be called with page parameter (if not, then default=0 means first page). The same is with moviesPerPage parameter which is default set to 20.
So, first time, we require first 20 movies so we will skip 0 (page * moviesPerPage = 0 * 20 = 0 ). And for the next page, we would want to skip the first 20 movies and see the next 20 movies. So: (page * moviesPerPage = 1*20 = 20). And ofcourse, we will limit the movies to moviesPerPage.

That is how this query will keep on working and pagination is achieved.

Kanika