这是一个根据Reddit的排名算法对项目进行排名的js代码.
我的问题是:如何使用此代码对我的mongodb文档进行排名?
(Reddit的排名算法)
function hot(ups,downs,date){ var score = ups - downs; var order = log10(Math.max(Math.abs(score), 1)); var sign = score>0 ? 1 : score<0 ? -1 : 0; var seconds = epochSeconds(date) - 1134028003; var product = order + sign * seconds / 45000; return Math.round(product*10000000)/10000000; } function log10(val){ return Math.log(val) / Math.LN10; } function epochSeconds(d){ return (d.getTime() - new Date(1970,1,1).getTime())/1000; }
Neil Lunn.. 8
那么你可以使用mapReduce:
var mapper = function() {
function hot(ups,downs,date){
var score = ups - downs;
var order = log10(Math.max(Math.abs(score), 1));
var sign = score>0 ? 1 : score<0 ? -1 : 0;
var seconds = epochSeconds(date) - 1134028003;
var product = order + sign * seconds / 45000;
return Math.round(product*10000000)/10000000;
}
function log10(val){
return Math.log(val) / Math.LN10;
}
function epochSeconds(d){
return (d.getTime() - new Date(1970,1,1).getTime())/1000;
}
emit( hot(this.ups, this.downs, this.date), this );
};
并运行mapReduce(没有reducer):
db.collection.mapReduce( mapper, function(){}, { "out": { "inline": 1 } } )
当然假设你的"集合"有字段ups
,downs
和date
.当然,"排名"需要以"独特"的方式发出,否则你需要一个"减速器"来整理结果.
但总的来说,应该做的工作.
那么你可以使用mapReduce:
var mapper = function() {
function hot(ups,downs,date){
var score = ups - downs;
var order = log10(Math.max(Math.abs(score), 1));
var sign = score>0 ? 1 : score<0 ? -1 : 0;
var seconds = epochSeconds(date) - 1134028003;
var product = order + sign * seconds / 45000;
return Math.round(product*10000000)/10000000;
}
function log10(val){
return Math.log(val) / Math.LN10;
}
function epochSeconds(d){
return (d.getTime() - new Date(1970,1,1).getTime())/1000;
}
emit( hot(this.ups, this.downs, this.date), this );
};
并运行mapReduce(没有reducer):
db.collection.mapReduce( mapper, function(){}, { "out": { "inline": 1 } } )
当然假设你的"集合"有字段ups
,downs
和date
.当然,"排名"需要以"独特"的方式发出,否则你需要一个"减速器"来整理结果.
但总的来说,应该做的工作.