Reputation: 676
I have below route
in my app.js
// Home Route
app.get('/', function(req, res){
Entry.find({}, function(err, entries){
if(err){
console.log(err);
} else {
res.render('index', {
entries: entries
});
}
});
});
This is displaying all entries in Entry collection. I would like to display only last 100 records from db. What is the best way to do it?
I am using with below pug
layout.
extends layout
block content
h1 #{title}
ul.list-group
each entry, i in entries
li.list-group-item
a(href=entry.entryurl)= entry.title
Upvotes: 0
Views: 171
Reputation: 722
Entry.find({}).sort({ "_id" : -1 }).limit(100).exec(function(err, entries) {
// Do something here
});
Upvotes: 2
Reputation: 8040
You could also use javascript to only send the last 100 items to the Pug layout:
// Home Route
app.get('/', function(req, res){
Entry.find({}, function(err, entries){
if(err){
console.log(err);
} else {
let lastHundred = entries.slice(-100);
res.render('index', {
entries: lastHundred
});
}
});
});
Upvotes: 1