We have learned about MongoDB's find () query function, in relational database selection (limit), sorting (sort) MongoDB also have, and use is more simple
First, let's take a look at adding a few document in
Now there are four document according to them, to the Limit Skip Sort respectively to learn the last a hodgepodge
1. Limit selection: How many I want to remove from these Document
Make a small example: I only need 2 Document
The result is obvious, very naked, very naked.
But I still have to explain: limit (2) is to select two document, starting from the entire collection of the first document to select two
What if we don't want to start with the first document?
2.Skip Skip: How many document I want to skip
A small example: I'm going to skip the first two document starting with the third document directly
The result is still obvious, very naked.
According to international practice to explain: Skip (2) is to skip two document, from the whole collection the first document started jumping, jump back two
Another example: Skip the first one and start with the second one directly.
The problem is, I just want the second and the third how to deal with it?
3.Limit + Skip: Pick from here to there
This is just the problem, a small example: I just want the second and third, how to deal with it?
International practice: Skip the first document select two document from the second article
Don't worry, there's another way.
The results are exactly the same for both, but the interpretation of international practice is different.
International practice: Select two document But to skip the first document select from the second article
Around the half-day, all dizzy, pay attention to the special attention here!!!!!! Here are two ways of writing, must be sure to remember one, because just remember one on the line, completely no difference, a Chinese understanding, one is the understanding of other countries
4. Sort order: Sort results by keyword
Take a small example: Find the document in ascending order by price | Descending arrangement
International practice: Ascending according to the Price field, 1 ascending, 1 descending
5. Limit + Skip + Sort mash up
One example: Select the second third and sort by price in ascending order
The problem arises, shouldn't it be 9800 and then 19800?
Here's the point of knowledge.
Emphasis: Sort + skip + Limit is the priority of their subsectors are priority sort followed by Skip last LIMT
Skip + Limit Priority is also the first skip and then limit
See once, can't remember
Look two times, it won't work.
See three times, do it yourself
Do it once, it's better to do it three times.
Practice a deeper impression
MongoDB Limit Select Skip Skip Sort Sort MongoDB-7