Now sorting is one thing we can do,
we can also skip a certain amount of elements.
Now why would you skip?
Let's say in your client application, a website, you implement some pagination, so people can view the
results which are distributed across multiple pages because you don't want to have one page with 100
results but you want to have 10 on every page.
Now if the user switches to page number two, you want to skip the first 10 results which would have been
on page number one but now you want to display the results for page number two
and therefore, you can add a skip method here to skip results.
So let's save and add skip ten here, you will see that the results here are slightly different,
we got 6.6 ratings
if you remember previously in the same amount of documents, we had 6.4 and so on,
so it looks like we skipped some documents and indeed we did.
So skipping does allow us to move through our data set,
if I skip 100, then we can see the ratings are much higher because we skipped all the bad ones, the 100
bad ones, we skipped them,
now skip is also used on a cursor as you can tell. Now related to skip somehow is the limit function, limit
allows you to limit the amount of elements the cursor should retrieve at a time
and that also means the amount of documents you can then move through with a cursor.
You can limit this to 10 for example,
the interesting thing is if you here add count, you still have 240
but if I now execute pretty here, indeed the cursor is not fully exhausted but I only get 10 elements
with the help of limit,
so limit also allows us to retrieve a certain amount of documents but only the amount of documents we
specify.
This also is related to pagination,
if you had your website with 10 products per page, it doesn't suffice to skip 10 products because you're
on page number two,
you also only want to display 10 products on page number two and that is exactly what limit helps you
do,
it limits the amount of data that is retrieved and returned through the cursor.
Now there is maybe one thing which you thought about,
if we have this query where we sort and I do actually sort twice here, I didn't notice that, as you see
it didn't do any harm but of course it is redundant,
so if we have a query where we sort and then we skip and then we limit, does the order matter?
In theory you would say it should matter right
because if I skip before I sort, then I skip on the unsorted documents,
so I simply skip the first 100 in the collection and I only sort the remaining ones which means I don't
necessarily skip the badly rated ones but I simply skipped some random items and I only sort after
skipping
which of course is typically not what you want.
Now the good thing is on a cursor, the order here does not matter,
later in the aggregation framework, this will change and I will talk about it there but here, you can
have skip, sort and limit in whichever order you want.
You can limit first, you can skip first,
mongodb and the cursor will automatically do it in the right order,
it will sort first, then skip and then limit.