[07:05:18] <ocx32> hello, i i have a counter in mongo, which is accessed by nodejs and incremented, now when multiple requests from node come in, the counter increment becomes inconsistent as multiple requests are reading the initial counter (shaow copy) and incremention only by 1, how can i solve this to have the counter blocking and read correcly?
[07:15:20] <ocx32> its not easy to switch just like that
[08:26:00] <ocx32> hello, i i have a counter in mongo, which is accessed by nodejs and incremented, now when multiple requests from node come in, the counter increment becomes inconsistent as multiple requests are reading the initial counter (shaow copy) and incremention only by 1, how can i solve this to have the counter blocking and read correcly?
[08:57:37] <ocx32> kali i am doing a mycol.update({'_id':x['_id']},{'$addToSet': {'channels': {'Name': ch} },'$inc': { 'available' : -1 } }) <-- if i get concurrent requests from node to do this, i am sometimes getting inconsistanies
[09:07:54] <kali> you should check the logs of your server, but you, or something is doing somehting wrong, the $inc is atomic.
[09:08:30] <ocx32> kali here channels is considered a subdocument ?
[09:11:17] <kali> i don't know what you mean, and it should not matter anyway
[09:13:05] <ocx32> kali here is my code https://pastebin.com/rx3nPcYL
[09:13:44] <ocx32> if a requests comes in while request1 is at line6, the counter becomes inconsistent
[09:16:41] <kali> if you're doing the obvious thing, you should just check available : { $gt: 0 } in the update command
[09:16:54] <kali> the inc is atomic. your find then update code is not
[09:18:06] <kali> and you need to check the update_one return to check that one document was updated
[09:18:22] <kali> you may actually be able to do anything in one single update
[09:21:49] <ocx32> kali how would it like what function should i use?
[09:22:52] <kali> let's just keep the find then update for now. add "available : { $gt: 0 }" beside the id on the update
[09:25:26] <kali> that should stop the inconsistency, but you also need to know if the update was successful, so you'll have to replace your update to findAndUpdate
[09:26:44] <kali> yeah, this will prevent the update to happen entirely if available got down to zero in the meantime
[09:27:26] <ocx32> , but you also need to know if the update was successful, so you'll have to replace your update to findAndUpdate <-- sorry didnt get that
[09:28:13] <kali> findAndUpdate is a variant of update that allow you to know if the update actually happen
[09:28:59] <ocx32> so it woulg go like mycol.findAndUpdate({'_id':x['_id'], "available : { $gt: 0 }"},
[09:29:15] <kali> if the available gets down to zero in between your find and your update (with the available check on update) the it will fails silently. your code has no way yo check. unless, instead of using update, you use a find and update AND check the outcome
[09:34:43] <kali> man, don't copy and paste, fix the syntax :)
[09:35:36] <ocx32> kali i have another one working on same document, happening on a different even : dbo.collection('servers').updateMany({ 'c': {'cName': pub } } , { $pull: {'c': {'cName': pC } } , $inc: {'available':1 } , $set: { 'status':'free'} } );
[09:55:03] <kali> ocx32: the sql world was comfortably settle in a paradigm from the late 60's and had difficulites accepting other people challenging it
[09:55:30] <ocx32> they said my problem cant be solved, only transactions can solve it which is no existent and complex in mongo
[10:13:19] <kali> in some setups, that were dramatically ignored by the old sql system, you will need to trade some persistence warranties for some performance
[10:13:54] <kali> mongodb lets you do that to some extent, while the old db are pretty radical on persistency
[10:14:31] <kali> once again, designing for a bank, not a web chat