Mongodb映射减少排序降序

Mongodb映射减少排序降序,mongodb,mapreduce,mongodb-query,Mongodb,Mapreduce,Mongodb Query,我收集了以下数据: { "_id" : "279771168740729_161573583988659_462046", "user_likes" : false, "message" : "good morning ICICI Bank have a great day...waiting for today surprise", "like_count" : 0, "message_tags" : [ { "id" : "279771168740729", "name" : "IC

我收集了以下数据:

    { "_id" : "279771168740729_161573583988659_462046", "user_likes" : false, "message" : "good morning ICICI Bank have a great day...waiting for today surprise", "like_count" : 0, "message_tags" : [ { "id" : "279771168740729", "name" : "ICICI Bank", "length" : 10, "offset" : 13, "type" : "page" } ], "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100002801855936", "name" : "Kowshik Krankz" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_161573583988659", "created_time" : "2012-11-03T04:10:31+0000" }
    { "_id" : "279771168740729_203743029752972", "icon" : "http://static.ak.fbcdn.net/rsrc.php/v2/yj/r/v2OnaTyTQZE.gif", "link" : "http://youtu.be/eKxIbLVRHRE", "page_username" : "icicibank", "caption" : "www.youtube.com", "from" : { "id" : "279771168740729", "category" : "Bank/financial institution", "name" : "ICICI Bank" }, "type" : "video", "updated_time" : "2012-07-18T04:32:24+0000", "shares" : { "count" : 40 }, "id" : "279771168740729_203743029752972", "message" : "Like Raghu, you too could be at the wrong place at the wrong time. But would you be able to clear your unpaid bills like Raghu did? Now you can! To know how, check out this video. For more details, visit http://bit.ly/NsoCY3", "picture" : "http://external.ak.fbcdn.net/safe_image.php?d=AQADR4-ELAVCbuSI&w=130&h=130&url=http%3A%2F%2Fi2.ytimg.com%2Fvi%2FeKxIbLVRHRE%2Fmqdefault.jpg", "source" : "http://www.youtube.com/v/eKxIbLVRHRE?version=3&autohide=1&autoplay=1", "status_type" : "shared_story", "likes" : { "count" : 643, "data" : [ { "id" : "100002247030669", "name" : "Angel Zoya" }, { "id" : "100002257585478", "name" : "Rakesh Kumar" }, { "id" : "100002062205767", "name" : "P.k. Choudhury" }, { "id" : "100000484071154", "name" : "Balaji Jadhvar" } ] }, "name" : "ICICI Bank", "page_id" : "279771168740729", "page_name" : "ICICI Bank", "created_time" : "2012-07-18T04:32:24+0000", "comments" : { "count" : 48 }, "actions" : [ { "link" : "http://www.facebook.com/279771168740729/posts/203743029752972", "name" : "Comment" }, { "link" : "http://www.facebook.com/279771168740729/posts/203743029752972", "name" : "Like" } ] }
    { "_id" : "279771168740729_203743029752972_572142", "user_likes" : false, "message" : ":-)", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "1060073189", "name" : "Raja Bhowmik" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_203743029752972", "created_time" : "2012-07-18T04:33:57+0000" }
    { "_id" : "279771168740729_203743029752972_572155", "user_likes" : false, "message" : "@?", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100001965306815", "name" : "Akhil Pandit" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_203743029752972", "created_time" : "2012-07-18T04:39:55+0000" }
    { "_id" : "279771168740729_203743029752972_572157", "user_likes" : false, "message" : "This ad is in very bad taste given the timing of it's release and the passing away of Satwik in the Bannerghata forests in Bangalore. Maybe there is no relation, but the similarity of the situation is uncanny.", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "588391958", "name" : "Vijay Alphonse" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_203743029752972", "created_time" : "2012-07-18T04:41:05+0000" }
    { "_id" : "279771168740729_203743029752972_572182", "user_likes" : false, "message" : "Lv 2 do job in a bank", "like_count" : 6, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100002492179903", "name" : "Monica Chandwani" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_203743029752972", "created_time" : "2012-07-18T04:48:51+0000" }

{ "_id" : "279771168740729_203743029752972_572228", "user_likes" : false, "message" : "R u working in ici bnk", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100002412887446", "name" : "Brijesh Gaur" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_203743029752972", "created_time" : "2012-07-18T05:10:06+0000" }
在这里,我需要根据喜欢的数量(like_count key的值)显示前2个帖子。因此,id为279771168740729_203743029752972_572182的帖子将排在第一位(6是最高的类似计数),id为279771168740729_203743029752972_572142的帖子排在第二位(4是次高的),依此类推

我提出了两个步骤:

  • 发出likeCount和postId
  • 按降序排序likeCount并显示前两个条目
  • 因此:

    var mapFunction = function() {
        var likeCount = this.like_count;
        var postId = this._id;
    
        if(postId != null && likeCount  !=  null){
            emit(likeCount, postId);
        }
    };
    
    var reduceFuntion = function(likeCount, postIdCollection) {
    /*How to maintain a single sorted list of likeCount and show the corresponding post?*/
    
    };
    

    我已经对mongo db文档中的排序功能感到困惑了。-请参阅post

    ,除非您确实计划使用MapReduce功能做其他事情,否则您最好只使用简单的Mongo查询。最好只使用查找查询:

    db.collectionName.find().sort({ like_count: -1 }).limit(2);
    
    如果您正在处理大量数据,我还建议您在like_count列上创建索引:

    db.collectionName.ensureIndex({like_count: -1})
    
    如果您真的很想使用map reduce,那么您可能需要使用map reduce命令上的sort an limit选项

    db.collectionName.mapReduce(mapFunction, reduceFunction, { sort: {like_count: -1, limit: 2}})
    
    它基本上对输入的数据集执行相同的查询,然后在输出的过程中对其进行切碎,然而,这意味着MapReduce步骤对您来说并不是很有用

    如果您想尝试使用纯MapReduce,那么您需要一种完全不同的方法来使用map和reduce函数。MapReduce进程在键上有一个隐式排序,这意味着您可以运行如下操作:

    var mapFunction = function() {
        var likeCount = - this.like_count;
        var postId = this._id;
    
        if(postId != null && likeCount !=  null){
            emit(likeCount,postId);
        }
    };
    
    var reduceFunction = function(a,b) {
        var out = b.join();
        return(a, out);
    };
    
    db.test.mapReduce(mapFunction, reduceFunction, {out: { inline: 1 }, limit: 2});
    
    然后处理结果集的最后一个成员,从末尾抓取条目,然后再向外展开以抓取帖子,不过需要对结果集进行一些展开以使其合理。注意,由于隐式排序顺序在增加,我们实际上发出的是负数,而不是正数,这意味着我们可以使用limit。这不是严格意义上的前两个帖子,而是前两个like_计数值和与之相关的所有帖子,因此您仍然需要一些后期处理

    当然,如果您想尝试其他方法,也可以使用聚合框架:

    db.collectionName.aggregate([{$sort: { like_count: -1 }}, {$limit: 2}]);
    

    我知道您建议的方法,但我想在MR wayUpdated中试用它,以包含一个基于map reduce的示例,该示例在map reduce中使用隐式排序。不过,我仍然认为您最好使用query方法进行实际使用:)。我执行了您的代码,请确认我理解的正确性-join函数创建了一个逗号分隔的集合,其中包含与计数相关的所有post id。还有一个步骤——提取前n个文档。我想finalize()函数是完成这项工作所必需的-您能为此提供任何输入吗?