用reddit排序算法对mongodb进行排序

编程入门 行业动态 更新时间:2024-10-23 18:22:32
本文介绍了用reddit排序算法对mongodb进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是根据Reddit的排名算法对项目进行排名的js代码。

Here is a js code to rank items according to Reddit's ranking algorithm.

我的问题是:如何使用此代码对我的mongodb文档进行排名?

My question is: how do I use this code to rank my mongodb documents ?

( 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; }

推荐答案

您可以使用mapReduce:

Well you can use 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(不带减速器):

And the run the mapReduce (without a reducer):

db.collection.mapReduce( mapper, function(){}, { "out": { "inline": 1 } } )

当然假设您的集合包含 ups ,缩减和日期。当然,排名需要以独特的方式发出,否则你需要一个减速器来分类结果。

And of course presuming that your "collection" has the fields for ups, downs and date. Of course the "rankings" need to be emitted in a way that is "unique" otherwise you need a "reducer" to sort out the results.

但一般来说应该做的。

更多推荐

用reddit排序算法对mongodb进行排序

本文发布于:2023-10-15 02:47:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1493019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   reddit   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!