我正在編寫一個新聞網站。我爲特定的作者/記者撰寫了專門的頁面。該頁面將包含他所有的職位/她。這裏的代碼:貓鼬:查找特定條件的文檔
在app.js,
app.get("/author/:id", function(req,res) { //latest contains all posts
Latest.find({author:"Richard Mann"}).sort([['_id', -1]]).exec(function(err,allLatest) {
if(err) {
console.log(err);
} else {
res.render("showAuthor", { latest : allLatest});
}
})
})
此代碼的工作,並通過特定作者的帖子出現。但如何爲所有作者做到這一點(同時避免DRY代碼)?在爲特定作者查找文檔時應該適用什麼條件?
如果您想通過'author'進行分組,您可以使用['$ group'](https://docs.mongodb.com/manual/reference/operator/aggregation/group/)聚合器。 – luisenrike