The most commonly used query option in MONGO is to limit the number of returned results, ignoring a certain number of results and sorting them. All of these options must be added before the query is distributed to the server. What's needed here is Limit,skip, Sort these three functions. Each of these three functions can achieve the goal of limiting the number of returned results, but there is a difference between them. 1.limit
to limit the number of results, you can use the Limit function after find. This function is similar to the number of pages per page of data, for example, if each page is 2 data, if the matching result is less than 2, then return the amount of matching results, if the match result is greater than 2, then return 2.
Limit-takes the first two data
db.getcollection (' activity ') of this table. Find ({}). Limit (2)
2.skip
the biggest difference between skip and limit is that skip is the first piece of data that meets the table data from the matching criteria, showing the data. For example, a total of four to meet the matching criteria, display the last two data.
Skip-takes the second two data db.getcollection (' activity ') of this table. Find
({}). Skip (2)
3.sort
sort takes an object as an argument: a pair of key/value pairs, the key corresponds to the document's key name, and the value represents the direction of the sort. The sort direction can be 1 (ascending), 1 (descending), and if more than one key is specified, the order of multiple keys is sorted.
For example: Follow the Activitycode field in ascending order, and you can write this:
Db.getcollection (' activity '). Find ({}). sort ({activitycode:1})
three function combinations use
These three functions can be used in combination and are useful for paging. For example: a merchant wants to query the table of activity, and according to Activitycode in ascending order, the following query statement can be used.
Db.getcollection (' activity '). Find ({}). Limit (2). Sort ({activitycode:1}) (without the query criteria added)
If a merchant wants to query Activitytype 1, and Sort by activitycode in ascending order, the following query statement can be used.
Db.getcollection (' activity '). Find ({"Activitytype": 0}). Limit (2). Sort ({activitycode:1}) (added query criteria)
A combination of three, can be understood as 2 pieces of data per page, click on "Next" can see more results, through skip can be very simple to achieve, only the first 2 can be introduced (already on the first page shown).
Db.getcollection (' activity '). Find ({"Activitytype": 0}). Limit (2). Skip (1). Sort ({activitycode:1})
if the number of skip is too high, it may cause performance problems and is not recommended. This will not reflect the advantages of MONGO. Summary: each of these three functions has its own usefulness, limit can be understood as the number of bars to be displayed on the first page of the page, and skip can be understood as the number of bars displayed on the next page, and sort can be understood as the field in which the page is sorted.