M220JS - Ticket: Paging, just not understand one sentence about totalNumMovies

try {

      const moviesList = await displayCursor.toArray()

Why totalNumMovies will be 0 if the page is not equal to 0? Not understand…

      const totalNumMovies = page === 0 ? await movies.countDocuments(query) : 0

      return { moviesList, totalNumMovies }

    } catch (e) {

      console.error(

        `Unable to convert cursor to array or problem counting documents, ${e}`,

      )

      return { moviesList: [], totalNumMovies: 0 }

    }

Hi @LU_YAO, welcome to the community.
The reason we are fetching the moviesCount just for the first page is because the UI has logic so that once it has a move count it doesn’t need to get a new movie count.
The page check is there because once we advance the page beyond 0, the count is known and there’s no need to count again which saves a database call.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like