This seems like a general database question, but I will ask it in my current context of NodeJs + MongoDb.
Say we have a universal counter with id "xyz". Here is a code snippet to get the last score and increase it by 1:
collection.find({id: "xyz"}, function(err, currentCount) { collection.update({id: "xyz"}, {$inc: {count: 1}}, function(err, data) { cb(currentCount); } } }
The problem is that if two clients try to call this code at the same time, then both clients can get the same account, then the collection updates the account twice - it is undesirable! How to fix it?
source share