I can't find where this is documented. By default, the find() operation will get the records from beginning.
router.get('/chat/get-messages', function(req, res) {
var db = req.db;
var collection = db.get('chatMessages');
collection.find({},{'limit':8},function(e,docs){
if (e) return next(e);
res.send(docs)
});
});
How can I get the N last inserted records ?
Sort by date
descending to get the last N records, and then call reverse()
on the docs
array to put them back in ascending order:
collection.find({}, {sort: {date: -1}, limit: 8}, function(e, docs){
if (e) return next(e);
res.send(docs.reverse());
});