M220N Advanced Reads

Ok… Question… I am looking at the AdvancedReads.cs in VS… And the Advanced reads Quiz…

Got one question on the .Limit() explanation. The answer states that the use of the limit limits the number returned… That I understand… But

 movies = await _moviesCollection.Find<Movie>(Builders<Movie>.Filter.Empty)
                .Skip(pageNumber * moviesPerPage)

This effectively limits your return by the number in the moviesPerPage veriable. If your return for movies was say 50 and your limit per page was 10, then the movies returned would be 40… So… Is the first 10 that are displayed the ones that are not included? Is the return only what isn’t displayed?

HI @David_Thompson, I am not really sure if I am getting your question correctly or not.
But to provide clarity on .limit(), here’s an explanation:
Suppose your movies collection has 50 documents, then:

The following command will return 10 documents:


The following command will only return 2 documents:


Since the first 48 were skipped and even if the limit was 10, it will only return the remaining documents(that is 2 documents in this case).

Also, one thing worth noting as well is that, both:

1.) db.movies.find(filter).sort(sortKey).skip(10).limit(10)


2.) db.movies.find(filter).sort(sortKey).limit(10).skip(10)

will return the same results, the query planner adjusts the limit and skip methods automatically.

I hope it helps.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Thanks for the response but you are correct in saying that you didn’t understand my question… Also I didn’t write it very well so let me expand on my quesiton.

I am trying to figure out how the .skip and .limit work together.

I understand the .Limit(moviePerPage) will limit the results to the page size but does it limit the actual results to the page size? Example page size is 10… Therefore it limits the results to 10?

Where I am trying to grasp is really in the next command. .Skip(pageNumber * moviesPerPage).
Now I know that page one on a 10 movie page size will return based on the .Limit right?

So if I have 50 movies that fit the querry, and my page size is 10… With the limit set to 10, does the querry only return 10? or does it return 50?

How does the .Skip(pageNumber * moviesPerPage) work on the initial display and when I press “next page” on the browser? Is it called again or is it just iterating the cursor?

Hi @David_Thompson,

Yes, it will limit the documents returned to 10.
Also, what do you mean when you say “actual results”?

Yup, even if the query matches with 50 docs, only 10 will be returned from the MongoDB server to your app.

No, it won’t iterate on the cursor, the query will be called again with all the new parameters, i.e. page=prevPage+1 and limit=10.

I hope it helps.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Ok. I understand that the query will be called again with the new paging information. So… Got an optimisation question for you… How do I limit the number of times this query takes place for like an online store where the customer that is browsing has the ability to slow response time down?

Even in containerised or service based designs, if the query is executed to the collection multiple times, this can adversely effect the performance of the program or application. Is there a way to bring the entire 50 records into memory and then just retrieve the ones desired based on any changing criteria (mainly the page number)

I am sure there is… And I am sorry, have to ask… LOL

Hi @David_Thompson, that’s an interesting question, and this appears to be a discussion that would benefit from broader visibility.

The MongoDB University categories support learners working through the free online courses at university.mongodb.com. The general categories have a wider readership, however questions or feedback on the course content is better discussed in the relevant course-specific categories.

It would be really great if you can post the above question in the Working with Data category.

Sourabh Bagrecha