<bdo id='6s1bk'></bdo><ul id='6s1bk'></ul>

      <i id='6s1bk'><tr id='6s1bk'><dt id='6s1bk'><q id='6s1bk'><span id='6s1bk'><b id='6s1bk'><form id='6s1bk'><ins id='6s1bk'></ins><ul id='6s1bk'></ul><sub id='6s1bk'></sub></form><legend id='6s1bk'></legend><bdo id='6s1bk'><pre id='6s1bk'><center id='6s1bk'></center></pre></bdo></b><th id='6s1bk'></th></span></q></dt></tr></i><div id='6s1bk'><tfoot id='6s1bk'></tfoot><dl id='6s1bk'><fieldset id='6s1bk'></fieldset></dl></div>

    1. <tfoot id='6s1bk'></tfoot>

    2. <small id='6s1bk'></small><noframes id='6s1bk'>

        <legend id='6s1bk'><style id='6s1bk'><dir id='6s1bk'><q id='6s1bk'></q></dir></style></legend>

        通过reddit排名算法对mongodb进行排序

        时间:2023-09-07

          <i id='v1f3l'><tr id='v1f3l'><dt id='v1f3l'><q id='v1f3l'><span id='v1f3l'><b id='v1f3l'><form id='v1f3l'><ins id='v1f3l'></ins><ul id='v1f3l'></ul><sub id='v1f3l'></sub></form><legend id='v1f3l'></legend><bdo id='v1f3l'><pre id='v1f3l'><center id='v1f3l'></center></pre></bdo></b><th id='v1f3l'></th></span></q></dt></tr></i><div id='v1f3l'><tfoot id='v1f3l'></tfoot><dl id='v1f3l'><fieldset id='v1f3l'></fieldset></dl></div>
          <tfoot id='v1f3l'></tfoot>
            <bdo id='v1f3l'></bdo><ul id='v1f3l'></ul>
            • <legend id='v1f3l'><style id='v1f3l'><dir id='v1f3l'><q id='v1f3l'></q></dir></style></legend>
                <tbody id='v1f3l'></tbody>

                  <small id='v1f3l'></small><noframes id='v1f3l'>

                • 本文介绍了通过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(不使用 reducer):

                  And the run the mapReduce (without a reducer):

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

                  当然,假设您的收藏"包含 upsdownsdate 字段.当然,排名"需要以独特"的方式发出,否则您需要一个reducer"来整理结果.

                  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.

                  但一般来说应该可以完成这项工作.

                  But generally speaking that should do the job.

                  这篇关于通过reddit排名算法对mongodb进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Couchdb 使用键连接两个文档 下一篇:Mongoose/MongoDB:计算数组中的元素

                  相关文章

                  最新文章

                • <legend id='NcZKj'><style id='NcZKj'><dir id='NcZKj'><q id='NcZKj'></q></dir></style></legend>
                  • <bdo id='NcZKj'></bdo><ul id='NcZKj'></ul>

                  <small id='NcZKj'></small><noframes id='NcZKj'>

                    1. <tfoot id='NcZKj'></tfoot>
                    2. <i id='NcZKj'><tr id='NcZKj'><dt id='NcZKj'><q id='NcZKj'><span id='NcZKj'><b id='NcZKj'><form id='NcZKj'><ins id='NcZKj'></ins><ul id='NcZKj'></ul><sub id='NcZKj'></sub></form><legend id='NcZKj'></legend><bdo id='NcZKj'><pre id='NcZKj'><center id='NcZKj'></center></pre></bdo></b><th id='NcZKj'></th></span></q></dt></tr></i><div id='NcZKj'><tfoot id='NcZKj'></tfoot><dl id='NcZKj'><fieldset id='NcZKj'></fieldset></dl></div>