admin管理员组文章数量:1290955
i have a site with multiple projects. and each project has a different view count.
What i want to do is to retrieve an array of the top 5 viewed projects in this order [max, max-1, max-2, max-3, max-4].
here's the schema:
var mongoose = require('mongoose');
// defines the database schema for this object
var schema = mongoose.Schema({
projectName : String,
authorName : String,
viewCount : Number
ment : [{
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,
ment : [{
id : projectJSONments.id,
authorName : projectJSONments.authorName,
authorEmailAddress : projectJSON.authorEmailAddress
});
project.save(function(err) {
if (err) {
console.log(err);
}
else{
console.log("success");
}
});
}
Below is my attempt to retrieve an array of the top 5 viewed articles in this order [max, max-1, max-2, max-3, max-4]
. Bearing in mind that articles view rank change in real time.
// because i am familiar with SQL, i start with a SQL query and convert it later to mongoose
SQL version:
SELECT MAX(viewCount) FROM project where projectName=1 --this only give the MAX when i want the top 5
mongoose version:
exports.getTopViewedProject = function(rank, callback)
ProjectModel.findOne({ projectName: 1 }).sort(viewCount, -1).run(
function(err, viewCOunt) {
var max = viewCount;
});
i have a site with multiple projects. and each project has a different view count.
What i want to do is to retrieve an array of the top 5 viewed projects in this order [max, max-1, max-2, max-3, max-4].
here's the schema:
var mongoose = require('mongoose');
// defines the database schema for this object
var schema = mongoose.Schema({
projectName : String,
authorName : String,
viewCount : Number
ment : [{
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,
ment : [{
id : projectJSON.ments.id,
authorName : projectJSON.ments.authorName,
authorEmailAddress : projectJSON.authorEmailAddress
});
project.save(function(err) {
if (err) {
console.log(err);
}
else{
console.log("success");
}
});
}
Below is my attempt to retrieve an array of the top 5 viewed articles in this order [max, max-1, max-2, max-3, max-4]
. Bearing in mind that articles view rank change in real time.
// because i am familiar with SQL, i start with a SQL query and convert it later to mongoose
SQL version:
SELECT MAX(viewCount) FROM project where projectName=1 --this only give the MAX when i want the top 5
mongoose version:
exports.getTopViewedProject = function(rank, callback)
ProjectModel.findOne({ projectName: 1 }).sort(viewCount, -1).run(
function(err, viewCOunt) {
var max = viewCount;
});
Share
Improve this question
edited Nov 8, 2012 at 19:35
bouncingHippo
asked Nov 8, 2012 at 18:55
bouncingHippobouncingHippo
6,04023 gold badges71 silver badges110 bronze badges
4
-
I'm a bit confused by your schema definition. Where's the
article
field that's in yourfindOne
call – JohnnyHK Commented Nov 8, 2012 at 19:27 - sorry about that, i edited it using the previous question schema – bouncingHippo Commented Nov 8, 2012 at 19:30
-
Thanks. You talk about wanting the top 5 articles but then you're querying for the max
viewCount
. Which one do you need help with? – JohnnyHK Commented Nov 8, 2012 at 19:36 -
i would like the top 5 projects please. i showed my work for the max
viewCount
to show at least i'm trying to think of a solution – bouncingHippo Commented Nov 8, 2012 at 19:37
1 Answer
Reset to default 9To get the top 5 articles for project 'name' by viewCount
:
ProjectModel.find({projectName: 'name'}).sort({viewCount: -1}).limit(5).exec(
function(err, projects) {
...
}
);
本文标签: javascriptmongoose query for top 5 viewed projects (sorted) using nodeJSStack Overflow
版权声明:本文标题:javascript - mongoose query for top 5 viewed projects (sorted) using nodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514141a2382782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论