简体   繁体   English

Mongoskin分页(跳过+限制)

[英]Pagination in Mongoskin (skip + limit)

From looking at mongodb examples, I tried a few things, such as: 通过查看mongodb示例,我尝试了一些操作,例如:

// returns an empty array
funnyPosts.find({limit:5}).toArray(function(err, result) {
    console.log(result);
});

Also tried 也尝试过

// returns { _construct_args: [],

console.log(db.collection('funny_posts').find().skip(3).limit(3));

Are you trying to implement pagination with Node.js and MongoDB? 您是否正在尝试使用Node.js和MongoDB进行分页?

Your parameters in the wrong place (not the first param to find() ). 您的参数放置在错误的位置(不是find()的第一个参数)。 Here is my code for HackHall GitHub link : 这是我的HackHall GitHub链接代码:

exports.getPosts = function(req, res, next) {
  var limit = req.query.limit || LIMIT;
  var skip = req.query.skip || SKIP;
  req.db.Post.find({}, null, {
    limit: limit,
    skip: skip,
    sort: {
      '_id': -1
    }
  }, function(err, obj) {
    if (!obj) next('There are not posts.');
    obj.forEach(function(item, i, list) {
      if (req.session.user.admin) {
        item.admin = true;
      } else {
        item.admin = false;
      }
      if (item.author.id == req.session.userId) {
        item.own = true;
      } else {
        item.own = false;
      }
      if (item.likes && item.likes.indexOf(req.session.userId) > -1) {
        item.like = true;
      } else {
        item.like = false;
      }
      if (item.watches && item.watches.indexOf(req.session.userId) > -1) {
        item.watch = true;
      } else {
        item.watch = false;
      }
    });
    var body = {};
    body.limit = limit;
    body.skip = skip;
    body.posts = obj;
    req.db.Post.count({}, function(err, total) {
      if (err) next(err);
      body.total = total;
      res.json(200, body);
    });
  });
};

For more info look up my other posts on webapplog.com and Express.js Guide . 有关更多信息,请在webapplog.comExpress.js Guide上查找我的其他文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM