在Mongoskin分页(跳过+限制)

从看mongodb的例子来看,我尝试了一些东西,比如:

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

也试过

 // returns { _construct_args: [], console.log(db.collection('funny_posts').find().skip(3).limit(3)); 

你是否试图用Node.js和MongoDB实现分页?

你的参数在错误的地方(而不是第一个参数find() )。 这里是我的代码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); }); }); }; 

欲了解更多信息,请查看我在webapplog.com和Express.js指南上的其他post。