Paging in getMovies()

Thank you @neillunn.

Giving the raw answer does not help the other person to learn.

Give a man a fish and you will feed him for the day, teach him to fish and you will feed him for the rest of his life.

Hi,
Someone please help, I am a starter in MongoDB; I am getting error when count the documents

@Saritha_36437 Your error is actually covered in response to one of the earliest threads. The test has a “timeout”. Change the timeout setting by following:

You possibly have an error in your code anyway if this test is timing out. Read the thread above and try to follow what it is saying.

FYI, Nothing in this implementation asked you to “count” anything. So it’s unclear if you think you are talking about a different question or whether you have misinterpretted the question as part of the problem here.

THIS QUESTION Is actually all about “skipping” through pages of data and “limiting” the number of results returned. Those were the only changes you were required to make in order to pass the test.

Thanks @neillunn !!!

@nathan.leniz can you help explain why the following (Exhibit A) works…

Exhibit A
const displayCursor = cursor.skip( page * moviesPerPage ).limit(moviesPerPage)

…but the following (Exhibit B) doesn’t?

Exhibit B
const displayCursor = cursor.skip( page > 0 ? ( ( page - 1 ) * moviesPerPage ) : 0 ).limit(moviesPerPage)

I lifted the skip portion directly from here.

Thanks in advance.

Okay, after having stared at Exhibit B for more time than I’d like to admit I’ve come to realize that the example in Mongo’s documentation is incorrect.

It should really be:

Summary
function printStudents(pageNumber, nPerPage) {
  print( "Page: " + pageNumber );
  db.students.find()
         .skip( pageNumber > 0 ? ( pageNumber * nPerPage ) : 0 )
         .limit( nPerPage )
         .forEach( student => {
           print( student.name );
         } );
}

Otherwise you would not skip any movies until you get to page 2. And even if you get to page 2 and beyond you’ll always be 20 movies behind.

This is what I get for blindly taking the example as gospel.

Hi @Shinsuke_73760,

I understand your confusion. I believe you are discussing about cursor.skip() documentation example here.

The code you mentioned is:

function printStudents(pageNumber, nPerPage) {
  print( "Page: " + pageNumber );
  db.students.find()
         .skip( pageNumber > 0 ? ( pageNumber * nPerPage ) : 0 )
         .limit( nPerPage )
         .forEach( student => {
           print( student.name );
         } );
}

So, suppose we are on 1st page, we will want to display the first 20(nPerPage) students.

pageNumber = 1
nPerPage = 20

So, if we go around your loop, the query looks like this:

   db.students.find()
         .skip( 20 )  // 1 * 20 = 20
         .limit( 20 )
         .forEach( student => {
           print( student.name );
         } );

So, it skips the first 20 students and then gives back students from id=21-40, but thats not expected to be shown on first page.

That is why we have .skip( pageNumber > 0 ? ( ( pageNumber - 1 ) * nPerPage ) : 0 ) this stage with “pageNumber - 1”.

Let me know if that helps.

Kanika

That really helped me… thanks! Always the answers to the labs are more obvious of what we think.

1 Like

stuck pipe for 5 hours…

When npm start the page in the app works, however there is an error and we can not register to get a note.

Hi @fcapelo,

Please share the pipeline you have used/modify for the ticket.

Kanika

@neillunn based hint.

Effectively you have to skip a number of movies.

But page doesn’t contain a number of movies.

If page = 0 (the first page) you have to skip 0 movies:
(moviesPerPage x page = 20 x 0 = 0).
If page = 1 (the second page) you have to skip 20 movies:
(moviesPerPage x page = 20 x 1=20)
If page = 2 (the third page) you have to skip 40 movies:
(moviesPerPage x page = 20 x 2=40)…

Without your comment I would been stuck. Thank you very much.