如何一次返回25个结果与mongoose

我创build了一个RESTful API,它将返回MongoDB集合中的文档。 如果是RESTful,我想限制返回到25的文档数量,然后让客户请求下一个25,然后下一个,直到所有文档都被读取。 使用find()我能够在一个集合中获得所有的文档,并使用find()。limit()我可以将其限制为25,但总是会获得前25个。是否有任何好的代码示例展示如何记住find()中的哪个位置,以便第二次调用find将返回集合中的下一个25个文档? 我的代码到目前为止…

var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { Transaction.find(function (err, transactions) { if (err) { mongoose.connection.close(); res.send('FAIL'); } else { mongoose.connection.close(); res.send(transactions); } }).limit(25); }); 

TX!

使用skip

 var recordsPerPage = 25; Transaction .find() .skip((currentPage - 1) * recordsPerPage) .limit(recordsPerPage) .exec(function (err, transactions) { res.send(transactions); }); 

skip将开始返回您传入的位置参数的结果。因此,如果您想要的结果是第3页(结果51到75),您只需跳过50个第一个结果。