MongoDB sort limit and skip usage

Source: Internet
Author: User

http://www.ttlsa.com/mongodb/mongodb-sort-limit-skip-usage/

1. Sort is used for sorting. Sort ({"Field": 1})

2. Skip skips how many results

3, limit each query returns the maximum number of results;

The use of the project is relatively good;

Record again;

The front desk passed, page parameters, according to the page parameter is determined to be the first few pages;

1 function (CB) {user.find (query). Skip (Page *). EXEC (function(err, items) {CB (err, items),});})
1Router.get ('/api/users/search/:key/:p age ',function(req, res) {2     if(_.isempty (Req.params.key)) {Res.json ({items:NULL});return; }3 4     varKey =Urlencode.decode (req.params.key);5     varpage =parseint (req.params.page);6     if(!page) page = 0;7     varquery = {Nickname:NewREGEXP (Key, "I")};8 Thunk.all ([9Thunkfunction(CB) {user.find (query). Skip (Page *).. EXEC (function(err, items) {CB (err, items);}); }),TenThunkfunction(CB) {user.count (query). EXEC (function(Err, count) {CB (err, count);}); }) One])(function(CB, results) { ARes.json ({items:results[0], total:results[1]}); -     }); -});

This is how the page page is usually paged

Careful analysis, should be click on which page, through the JS to get the number of clicks, the number of pages through Ajax or direct jump to the background, back to the background data, rendering to the front;

Skip above refers to skipping the page*40 bar,

Limit returns 40,

For example, page fifth, skip 4*40, showing 161 to 200 of the content;

MongoDB sort limit and skip usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.