There is a collection of "counter" with the field "value", which stores a natural number. There is a button on the web page that, when clicked, sends a post request to a handler that is connected to mongodb. When a request arrives, this handler adds a new record to the collection, which is equal to db.counter.find (). Length () + 1. By the condition, each new record contains a value that is one greater than the value of the previous record, and they all go in order. In the case of a single user pressing a button, this logic will work, however, in the event of the appearance of a second user, there is a risk of simultaneously pressing the button and duplicating the value of db.counter.value. And by the condition, I can not use the lock on collections / documents. Such a collision, in what ways can I avoid it?
- This is impossible without pessimistic or optimistic blocking. You can assign it to Mongu (using a unique index ) or perform it yourself using an analogue of the CAS operation (insert if not exists, you could not find it quickly). In any case, you probably do auto-increment on the knee, which you don't really need at all - it's better to use a UUID. - etki
- @Etki that's right, you need your own increment. Roughly speaking, I need a human-readable id for an entity, of the form / ^ [0-9] {1.12} $ /. Thank you, read about indexing. - Matvey Safronov
|