我有一个多项目的网站。每个项目都有不同的视图计数。我想要做的是以这个顺序检索前5个已查看项目的数组( [max, max-1, max-2, max-3, max-4]. )。
下面是模式:
var mongoose = require('mongoose');
// defines the database schema for this object
var schema = mongoose.Schema({
projectName : String,
authorName : String,
viewCount : Number
comment : [{
id : String,
authorName : String,
authorEmailAddress : { type : String, index : true }
}]
});})
// Sets the schema for model
var ProjectModel = mongoose.model('Project', schema);
// Create a project
exports.create = function (projectJSON) {
var project = new ProjectModel({
projectName : projectJSON.projectName ,
authorName : projectJSON.authorName,
viewCount : projectJSON.viewCount,
comment : [{
id : projectJSON.comments.id,
authorName : projectJSON.comments.authorName,
authorEmailAddress : projectJSON.authorEmailAddress
});
project.save(function(err) {
if (err) {
console.log(err);
}
else{
console.log("success");
}
});
}下面是我按[max, max-1, max-2, max-3, max-4]顺序检索前5位查看文章的数组的尝试。请记住,文章实时查看排名变化。
// because i am familiar with SQL, i start with a SQL query and convert it later to mongoose
SQL版本:
SELECT MAX(viewCount) FROM project where projectName=1 --this only give the MAX when i want the top 5
猫鼬版本:
exports.getTopViewedProject = function(rank, callback)
ProjectModel.findOne({ projectName: 1 }).sort(viewCount, -1).run(
function(err, viewCOunt) {
var max = viewCount;
});发布于 2012-11-08 19:39:03
通过viewCount获取项目“名称”的前5篇文章
ProjectModel.find({projectName: 'name'}).sort({viewCount: -1}).limit(5).exec(
function(err, projects) {
...
}
);https://stackoverflow.com/questions/13295820
复制相似问题