[英]List last N record from MongoDB collection
我的app.js
中有以下route
// Home Route
app.get('/', function(req, res){
Entry.find({}, function(err, entries){
if(err){
console.log(err);
} else {
res.render('index', {
entries: entries
});
}
});
});
這將顯示條目集合中的所有條目。 我只想顯示數據庫中的最后 100 條記錄。 最好的方法是什么?
我正在使用下面的pug
布局。
extends layout
block content
h1 #{title}
ul.list-group
each entry, i in entries
li.list-group-item
a(href=entry.entryurl)= entry.title
Entry.find({}).sort({ "_id" : -1 }).limit(100).exec(function(err, entries) {
// Do something here
});
您還可以使用 javascript 僅將最后 100 個項目發送到 Pug 布局:
// 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
});
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.