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)
                .Sort(sortByYearDescending)
                .Limit(moviesPerPage)
                .Skip(pageNumber * moviesPerPage)
                .ToListAsync();

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:

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

The following command will only return 2 documents:

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

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)

and

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