Skip to content

Instantly share code, notes, and snippets.

@kosmikko
Last active January 21, 2016 10:01
Show Gist options
  • Save kosmikko/1c3a467cbd5934ad37b7 to your computer and use it in GitHub Desktop.
Save kosmikko/1c3a467cbd5934ad37b7 to your computer and use it in GitHub Desktop.
Credits transfer (find issues with it)
function transferCredits(from, to, amt) {
var fromAccount = db.game_accounts.findOne({"name": from},{"credits": 1});
var toAccount = db.game_accounts.findOne({"name": to},{"credits": 1});
db.game_accounts.update({name: from}, {$set: {credits: fromAccount.credits - amt}});
db.game_accounts.update({name: to}, {$set: {credits: toAccount.credits + amt}});
}
db.game_accounts.insert({name: "John", credits: 1000});
db.game_accounts.insert({name: "Jane", credits: 1000});
// John transfers credits to Jane
transferCredits("John", "Jane", 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment