I have an app with sweepstakes. The user simply has to click a button, a request for an API is sent to Express, the user is entered as participating in the MongoDB database and will respond to the user about the successful execution of the request.

But if you send two such requests at the same time, the user can get into the same distribution two or more times.

How can this be avoided? Is there a good way to store an array with users for whom such a request is already being processed? And if the varieties of such requests are many?

Closed due to the fact that the essence of the question is unclear by the participants deivan_ , user194374, lexxl , Vartlok , aleksandr barakin 10 Aug '16 at 7:37 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    You can add a restriction to the collection of practical jokes on the uniqueness of the username field:

    db.lottery.createIndex( { "name": 1 }, { unique: true } ) 

    After that, an attempt to insert a document with a non-unique name field will cause an error, which means that this user is already participating in the drawing. As a bonus, you can be sure that the collection is always unique users, even if they are added by some other script (written by another programmer).