Answer the question
In order to leave comments, you need to log in
What is the correct way to save mongoose document in this case?
Let there be a document with a numeric parameter. And the server receives requests to change this parameter (n + 1 or n-1) several times a second.
Let's say we received 2 requests almost simultaneously and ran the processing function 2 times. The function gets the document, substitutes a new number and saves. But due to asynchrony and a delay in the execution of the script, it may turn out that when #2 request has already been processed and the document has been saved, #1 is still in progress and after its completion we will lose the result of processing request #2. How to solve this problem?
Alternatively, create a new special document for each request, and then process them all synchronously once a minute, save the result to the main document and delete the special ones. Are there other ways?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question