Skip to content

Instantly share code, notes, and snippets.

@iron9light
Created February 10, 2012 06:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iron9light/1787238 to your computer and use it in GitHub Desktop.
Save iron9light/1787238 to your computer and use it in GitHub Desktop.
Get random records form mongodb via map/reduce
function(k, v) {
return v.a ? v.a.map(function(x) {
return x.k
}) : [v.k]
}
function(k,v){return v.a?v.a.map(function(x){return x.k}):[v.k]}
function() {
emit(0, {k: this, v: Math.random()})
}
function(){emit(0,{k:this,v:Math.random()})}
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*/)};
}
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