I'm trying to prevent concurrent requests to a specific record, see the following example:
function addMoney(orderID,orderID){
const status = Database.getOrder(orderID);
if (status === 1){
return "Money Already Added";
}
Database.udateOrder(orderID, {status: 1});
Database.addMoney(userID, 300);
return true;
}
Assume someone made this request exactly at the same time, therefore the "status" check passed, they'd be able to get Database.addMoney
run twice.
Using MySQL, I'd start a transction to lock the row but not sure how to do so using MongoDB.