Created
May 8, 2011 22:25
-
-
Save yitsushi/961745 to your computer and use it in GitHub Desktop.
How to order it ^_^ Just use mapReduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "init.php"; | |
$map = new MongoCode("function() { | |
this.comments_count = this.comments.length; | |
if (this.likes) this.likes_count = this.likes.length; | |
else this.likes_count = 0; | |
emit(this, {count: 1}); | |
}"); | |
$reduce = new MongoCode("function(k, vals) { return 1; }"); | |
$sales = $wsCache->command(array( | |
"mapreduce" => "wall", | |
"map" => $map, | |
"reduce" => $reduce, | |
"out" => array("merge" => "wallCounts"))); | |
$posts = $wsCache->selectCollection($sales['result'])->find()->sort( | |
array('_id.likes_count' => -1, '_id.comments_count' => -1) | |
); | |
foreach ($posts as $post) { | |
$post = $post['_id']; | |
echo "{$post['_id']} has {$post['comments_count']} comment(s) and {$post['likes_count']} like(s).\n"; | |
} | |
/* | |
* OUTPUT: | |
* | |
* 4db8762fee60c6951b000000 has 0 comment(s) and 1 like(s). | |
* 4db87648ee60c6951b000002 has 0 comment(s) and 1 like(s). | |
* 4d984f2eee60c63e1d000031 has 3 comment(s) and 0 like(s). | |
* 4db8774dee60c68318000006 has 3 comment(s) and 0 like(s). | |
* 4db872ebee60c68318000002 has 2 comment(s) and 0 like(s). | |
* 4d913020ee60c6843200000c has 1 comment(s) and 0 like(s). | |
* 4db87475ee60c68318000003 has 1 comment(s) and 0 like(s). | |
* 4d984f27ee60c6cd1b00002f has 0 comment(s) and 0 like(s). | |
* 4db874e7ee60c6101b000001 has 0 comment(s) and 0 like(s). | |
* 4db87511ee60c67919000003 has 0 comment(s) and 0 like(s). | |
* 4db87639ee60c6951b000001 has 0 comment(s) and 0 like(s). | |
* 4db8772bee60c6b21e000000 has 0 comment(s) and 0 like(s). | |
* 4db8772dee60c63d0c00000a has 0 comment(s) and 0 like(s). | |
* 4db8772fee60c6ae1e000001 has 0 comment(s) and 0 like(s). | |
* 4db87748ee60c68318000004 has 0 comment(s) and 0 like(s). | |
* 4db8774bee60c68318000005 has 0 comment(s) and 0 like(s). | |
* 4db8774eee60c68318000007 has 0 comment(s) and 0 like(s). | |
* 4db87750ee60c68318000008 has 0 comment(s) and 0 like(s). | |
* 4db87752ee60c68318000009 has 0 comment(s) and 0 like(s). | |
* 4db87754ee60c6831800000a has 0 comment(s) and 0 like(s). | |
* 4db87756ee60c6831800000b has 0 comment(s) and 0 like(s). | |
* 4db87758ee60c6831800000c has 0 comment(s) and 0 like(s). | |
* 4db8775aee60c6831800000d has 0 comment(s) and 0 like(s). | |
* | |
*/ |
the best way:
create a property what count the specified entries ($inc) because you can create an index.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$wsCache is a mongoDB object.