Created
February 10, 2012 06:43
-
-
Save iron9light/1787238 to your computer and use it in GitHub Desktop.
Get random records form mongodb via map/reduce
This file contains 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
function(k, v) { | |
return v.a ? v.a.map(function(x) { | |
return x.k | |
}) : [v.k] | |
} |
This file contains 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
function(k,v){return v.a?v.a.map(function(x){return x.k}):[v.k]} |
This file contains 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
function() { | |
emit(0, {k: this, v: Math.random()}) | |
} |
This file contains 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
function(){emit(0,{k:this,v:Math.random()})} |
This file contains 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
function(k, v) { | |
var a = [] | |
v.forEach(function(x) { | |
a = a.concat(x.a ? x.a : x) | |
}) | |
return {a:a.sort(function(a, b) { | |
return a.v - b.v; | |
}).slice(0, 3 /*how many records you want*/)}; | |
} |
This file contains 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
function(k,v){var a=[];v.forEach(function(x){a=a.concat(x.a?x.a:x)});return{a:a.sort(function(a,b){return a.v-b.v}).slice(0,3)}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment