There is an application written on node expressjs, the base is used by mongodb.
The algorithm is as follows: when the post comes in, a record is searched for in the fields name , server , fraction . If there is such an entry, update, if not, create one.
The problem is that with a large number of requests, if it does not find a record, it creates not one record, but several. I assume that while mongodb creates a record, the next search does not give results, because the record has not yet been created and the search does not give results, and mongodb creates another identical record, although it has to update.
How to solve this problem?
insertOne(orinsertMany) with the parameterupsert: true. Judging by the description, it does the same thing as described in the question ("Если такая запись есть то обновить, если нет то создать."), But not by means of an external code, but by the base itself. It is more than likely that in this case duplicates will not be created. Let knowledgeable people confirm or deny. :) - Yaantupdatealso has a flagupsert, to this my quick search did not bring me. :) So you need theupdate. :) - Yaant