Des*_*ado 5 mongoose mongodb node.js express
我用 Express 和 Mongoose 制作了一个评论应用程序。我有一个像下面这样的评论模型:
var mongoose = require('mongoose');
var ReviewSchema = mongoose.Schema({
title: String,
description: String,
rating: Number
}, {
timestamps: true
}
);
module.exports = mongoose.model('Review', ReviewSchema);
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我只得到如下所有评论列表。但现在我想得到一个包含 10 条最近评论和排序方式的列表(orderby 时间戳)。我怎样才能用猫鼬做到这一点?请帮我!我是 NodeJS 和 Mongodb 的新手。
exports.findAll = function(req, res) {
console.log("Fetching Review...")
// Retrieve and return all reviews from the database.
Review.find(function(err, reviews){
if(err) {
console.log(err);
res.status(500).send({message: "Some error occurred while retrieving Review."});
} else {
res.send(reviews);
}
});
};
Run Code Online (Sandbox Code Playgroud)
非常感谢
Akr*_*ion 13
这应该适合你:
Review.find()
.sort({_id: -1})
.limit(10)
.then(reviews => {
console.log(reviews)
});
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以这样尝试:
Review.find({}, function(err,reviews){}).sort({_id: -1}).limit(10);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8771 次 |
| 最近记录: |