Last active
December 15, 2015 04:29
-
-
Save junosuarez/5201918 to your computer and use it in GitHub Desktop.
the database script used to pick a winner for the showfort.com VIP pass giveaway.
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
// all right showfortians, let's pick a winner! here's the actual source code: | |
// first let's load our database driver & connect to it: | |
var minq = require('minq') | |
minq.connect(process.env.CONNECTION_STRING) | |
// in order to be eligible, you need to have favorited at least one show | |
// and not be one of my own accounts | |
var eligible = { | |
'faves.shows': {$not: {$size: 0}}, | |
'name': {$nin: ['jden', 'FREEshows', 'test123']} | |
} | |
// out of the total number of eligible users: | |
minq.from('users').where(eligible).count().then(function (count) { | |
// we want to pick a random one | |
var random = Math.floor(Math.random() * count) | |
minq.from('users').where(eligible) | |
// in databases, we do this like playing duck-duck-goose, where we skip | |
// a certain number of ducks to get to the lucky goose. | |
.skip(random) | |
// just one winner :) | |
.limit(1) | |
// now we just need to update that user account and make it as a winner! | |
.update({$set: {winner: true}}) | |
.then(function () { | |
// That's all there is to it! Next time you log in to showfort, | |
// the winner might be you :) | |
console.log('We have a winner...') | |
}) | |
}) |
phew. that was close.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WAIT false alarm people. let me fix that before actually running it :) we need to skip a random number, not 0